Does curly braces use against the "Ruby way"?

I have been using Ruby for about two weeks, and I have not programmed for too long, but I am switching to a language from a C-style background (C ++, C #, etc.). In any case, a good friend and my mentor looked at some Ruby that I wrote the other day, and he told me that he would hit me if he caught me again using curly braces in Ruby.

Well, I just found out about Builder yesterday through this About.com article , and the curly braces are used in the example they published. Is there any other way to do this, or do you need to use curly braces with Builder?

This may seem secondary, but I'm new to Ruby, and I don't want to allow myself to develop any bad habits. What do you guys think?

+7
source share
4 answers

While some people come with โ€œcurly braces for single-line, for end-users,โ€ I personally find the following rule to be the most logical:

  • use do-end when your block has side effects (usually using each and its associated methods) and
  • use braces when your block has no side effects ( map , inject and so on)

This logic goes well with the method chain problem Matt wrote about.

One of the advantages of this approach is that it will make you think of side effects every time you write a block, and they are very important, although sometimes they are not missed by encoders that do not have functional programming.

Another way to express this, without using side effect terminology, would be as follows:

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

Here are some articles with more information:

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

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

+10
source

Idiomatic ruby

 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 the chain, foo.map {|f| f.num}.reduce(0) {|memo, i| memo + i} foo.map {|f| f.num}.reduce(0) {|memo, i| memo + i} foo.map {|f| f.num}.reduce(0) {|memo, i| memo + i} looks prettier, and then posts an end method call like

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

There is something strange about calling the end method, although syntactically the two are equivalent.

+10
source

The usual convention is { ... } blocks for single-line and do ... end for multi-line. I usually follow this convention, but should I ever be king, I think I would use do .. end more often.

The random problem with {} is that {} bound more tightly than do end , so the only way to write a poetry style block for a method that also has parameters is to use do end , otherwise the block will be part of the parameter and will not be passed directly to the method.

 def fx yield x end f 123 do |n| pn end # works f 123 { |n| pn } # does not work f(123) { |n| pn } # works, of course 

Of course, if you want to attach a block to a parameter in poetry mode, then you will win with {} , followed by a do end .

 def g ; p ['g', yield] end def fx; p ['f', yield] end fg { 2 } do 3 end ["g", 2] ["f", 3] 

Lastly, and unlike some of the tips you got here ; not required until end .

+4
source

The Ruby Way does not protect any particular style. If they did not want you to be able to use curly braces, this would not be in the language. However, stick to the command line style (if you are working with a command).

In most cases, the Ruby code I've seen uses end instead of curly braces, but I don't use Ruby, so I can't say for sure that this is the preferred style. Ruby has more than one way of doing things - you can use whatever method you like (within reason).

-2
source

All Articles