Fenghe's Blog

Ruby Basics – Comments and Loop

I learned Ruby in 2018, it is a really powerful and reflexible language. This is a tutorial Series on Ruby on Rails.

Today, it will be about comments and loops in Ruby.

Comments

# This is a comment in Ruby

Loops

To output

1
2
3

There are several ways:

times

3.times {|i| puts i}   # i is the loop variable

For loop

for i in 0..2
    puts i
end

while

i = 3
while i > 0
    puts a
    i -= 1
end

until

i = 3
until i >= 0
    puts a 
    i -= 1
end

References

  1. https://github.com/jhu-ep-coursera/fullstack-course1-module2/tree/master/Lecture01-Ruby-Basics
  2. https://github.com/jhu-ep-coursera/fullstack-course1-module2/blob/master/Lecture02-Control-Flow/for_loop.rb
  3. https://github.com/jhu-ep-coursera/fullstack-course1-module2/blob/master/Lecture02-Control-Flow/while_until.rb

Leave a Reply

Your email address will not be published. Required fields are marked *