Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using curly braces go against the "Ruby way"?

I've been using Ruby for about two weeks, and I've not been programming for too terribly long, but I'm coming at the language from a C-style background (C++, C#, etc). Anyway - a good friend and mentor of mine was looking at some Ruby that I'd written the other day, and he told me that he'd smack me if he caught me using curly braces in Ruby again.

Well, I just found out about Builder yesterday, via this About.com article, and the example that they have posted uses curly braces. Is there a different way to do this, or do you have to use curly braces with Builder?

This may seem like a minor point, but I'm new to Ruby, and I don't want to let myself develop any bad habits. What do you guys think?

like image 918
eckza Avatar asked Mar 02 '11 16:03

eckza


People also ask

Does Ruby use curly braces?

Ruby blocks are anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or curly braces {}.

Where do curly braces go?

For methods, the opening curly brace is on a separate line. But for other blocks (such as the "if" statement), the curly brace is on the same line.

What are the main purpose of curly brace?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

Can we skip the curly braces?

So we can omit curly braces only there is a single statement under if-else or loop. Here in both of the cases, the Line1 is in the if block but Line2 is not in the if block. So if the condition fails, or it satisfies the Line2 will be executed always.


2 Answers

While some people go with "braces for one-liners, do-end for multi-liners", I personally find the following rule the most logical:

  • use do-end when your block has side-effects (typically, with each and related methods) and
  • use braces when your block is without side-effects (map, inject and alike)

This logic goes well with method chaining issue that Matt wrote about.

One benefit of this approach is that it is going to make you think about side-effects every time you write a block, and they are very important, although sometimes overlooked by coders with no functional programming background.

Another way to put it, without involving side-effects terminology would be:

  • use do-end for blocks that perform
  • use { and } for blocks that return

Here are couple of articles with more info:

http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc

http://talklikeaduck.denhaven2.com/2007/10/02/ruby-blocks-do-or-brace

like image 54
Mladen Jablanović Avatar answered Sep 25 '22 23:09

Mladen Jablanović


Idiomatic ruby is

method_name {|param| param.do_something} # squigglies for one liners

# do/end for multi-line
method_name do |param|
  param.do_something
end

One reason for this is for chaining, foo.map {|f| f.num}.reduce(0) {|memo, i| memo + i} looks nicer then hanging a method call off of an end like

foo.map do |f| 
  f.num
end.reduce(0) do |memo, i| 
  memo + i
end

There is just something strange about calling a method off of end, even though syntactically the two are equivalent.

like image 27
Matt Briggs Avatar answered Sep 24 '22 23:09

Matt Briggs