General question regarding the {} instead of do/end


#1

It was explained during the module that the do...end commands where interchangeable with the { ..} command. However, when trying this with the .while looping with while module, it continues to return a syntax error.

x = 1

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

Any reason why this syntax error occurs when the syntax is correct for the do...end command? Would appreciate some insight :smile:

x = 1

while x < 51 do
print x
x += 1
end


#2

Hi Joeie123,

I did a quick search on whether or not it was possible to use curly brackets ({}) in Ruby, and got this:

The answer everyone gave was no, it's not possible to use curly brackets in Ruby. That was for an if statement in Ruby, but I think the same thing will apply to the do...end loops.
Could you show me where it says that? (preferably with a link to the place)
Thanks!


#3

Hi zystvan,

This was the quote from the module 5.8 The Loop Method.

loop { print "Hello, world!" }
In Ruby, curly braces ({}) are generally interchangeable with the keywords do (to open the block) and end (to close it). Knowing this, we can write a smarter loop than the one above:

i = 0
loop do
i += 1
print "#{i}"
break if i > 5
end

The link is available here. https://www.codecademy.com/courses/ruby-beginner-en-XYcN1/2/1?curriculum_id=5059f8619189a5000201fbcb

It did seem to work when used in that particular problem but returned a syntax error when it was used with the while and until commands in 5.14 and 5.15


#4

@joeie123 Sorry I didn't respond sooner.
Looks like the curly brace version should work to me, but I'm not a Ruby expert. Maybe someone else could help you?
@arjofocolovi do you mind?
Thanks :)


#5

I'm far from a ruby expert either, my only experience in the language is this course and a small research on my own.

It seems that the keywords until and while only accept do/end, and I couldn't find an answer as to why.

I suggest asking the question on www.stackoverflow.com (I've made some research on the existing threads but couldn't find this question answered).