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


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}"

what does the "do" signify.

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


   # code in the block

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.



are do and end only used for loops ?


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.


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