What does the "do" and "end mean in ruby. -- I am new and trying to learn as much as i can


#1

What does the "do" and "end mean in ruby ?

for instance:

restaurant_menu = {
"noodles" => 4,
"soup" => 3,
"salad" => 2
}

restaurant_menu.each do |item, price|
puts "#{item}: #{price}"
end

what does the "do" signify.

thanks in advance for any help. I am new to this.


#2

do
   # code in the block
end

The above represents a contained block of code that executes according to the controls applied. In the .each loop it is optional, but it still marks out the block executed N times in the loop.


#3

thanks

are do and end only used for loops ?


#4

They are only block delimiters. do is not a loop construct, it must be paired up with a control method, such as .each or a conditional such as while or loop until. Keep studying and reading and all this will unfold.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.