CodeAcademy – Ruby 3

学んだこと:while ~~, for ~ in 1..5 / 1…5, next if

↓ Loops and iteration

i = 1
for i in 1..20
puts i
i += 1
end

i = 20
loop do
i -= 1
next if i % 2 == 1
print “#{i}”
break if i <= 0
end

FizzBuzz 問題について自分が書いたコード。
(Codeacademy を20%くらいやれば自分で解けるようになる)

i = 1
for i in 1..100

 

 

if i % 15 == 0
puts “FizzBuzz

 

 

elsif i % 3 == 0
puts “Fizz”

elsif i % 5 == 0
puts “Buzz”

elsif puts i

 

i += 1
end
end

 

 

Codeacademy – Ruby 2

学んだこと、 if, gets.chomp, include?, gsub!(/~~/, “~~~”)

↓Thith Meanth War! (Codeacademy)

print “type something”
user_input = gets.chomp
user_input.downcase!

 

 

if user_input.include? “s”
user_input.gsub!(/s/, “th”)
else
puts “you didn’t type s”
end

 

puts “Replacing s, you typed #{user_input}”

Codeacademy – Ruby

Codeacademy でRuby のレッスンを初めてみた。
Rubyインストールについてはこちらを参照

↓ Printing the output

print “What’s your first name?”
first_name = gets.chomp.capitalize!

print “What’s your last name?”
last_name = gets.chomp.capitalize!

print “What’s your city?”
city = gets.chomp.capitalize!

print “What’s your state?”
state = gets.chomp.upcase!

puts “Your name is #{first_name} #{last_name}, and you are from #{city}, #{state}”