Same code with { } leads to error, while ‘do’ and ‘end’ do work --> when are/aren't they interchangeable?


#1

https://www.codecademy.com/en/courses/ruby-beginner-en-XYcN1/3/1?curriculum_id=5059f8619189a5000201fbcb

I wrote my code for this lesson with with { and } and it lead to an error message. Replacing { and } by ‘do’ and ‘end’ (not changing anything else in my code) made the code work perfectly well!
From lesson 8: ‘In Ruby, curly braces ({}) are generally interchangeable withthe keywords do (to open the block) and end (to close it).’ So I'm now trying to find out: when are and when aren’t they interchangeable? (I'm kind of lazy, so always trying to find the easiest/shortest way to do something - I love { and } !)

i = 1
while i < 51 {
  print i
  i += 1
}

Error message: 
(ruby):1: syntax error, unexpected '{', expecting keyword_do_cond or ';' or '\n'
(ruby):4: syntax error, unexpected '}', expecting $end

#2

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