5/8 Syntax explained


#1

I just want to understand proper ruby with this question.

I have submitted the 2 following lines of code on this particular challenge and both have given me a clear. Can someone explain why this is?

words.each { |word| frequencies[word] += 1 }

words.each do |word| frequencies[word] += 1
end

Notice one of these has curly braces while the other includes the keywords do and end, but no braces. Why is this possible? Just trying to wrap my head around the small things to help me understand this language more.

Thanks!


#2

Ruby was created to be read and typed to understand easily. For programmers who are more accustomed to using Curly brackets that's an option, while other who want to read things easier can use the "do / end" Do and End are like { and } to ruby.

The same way that you can say "13.times do /*code*/ end" It's really easy to read from left to right and would work the same way if you used the curly brackets.


#3

I just saw a different example of this and I totally get it!

Appreciate your response though!


#4

Great question! And a great answer! :slight_smile: