From: Jeremy McAnally Date: 2007-01-10T09:18:30+09:00 Subject: Re: [ANN] Elements of Ruby Style > 2.b.vi. If a your method has a block parameter, try to use yield rather > than accepting it as a variable and calling call on it. > --> What is the reasoning behind this? I'm not trying to criticize (yet > ;-), but it seems like The Ruby Way is not doing this. For example, $_ > = "hollywood"; scan(/o/); isn't preferred over "hollywood".scan(/o/). My personal reasoning is that you end up with less confusion as to what's going on when simply looking at the code. You know that yield yeilds, but an object may have a call method. Secondly, when calling the method, it's cleaner when calling. You get: mymethod { puts "Go go gadget method!" } as opposed to... mymethod(lambda { puts "Go go gadget lambda!" }) Just personal preference I suppose. > 2.c.ii says "curly braces" while 2.c.i just says "braces." Also, what > if you want something's return value but you do it on multiple lines? > Either that never happens, so the rule isn't necessary, or it happens > and then you have a conflict in the rules. > True, I'll fix that. > 2.e.i Use try catch to control flow rather than rescue blocks > --> Do you mean catch/throw? I don't recall a try/catch in Ruby and a > quick Googling finds nothing. > You're right; that's C# on the brain (and shows how often I use catch/throw ;)). > 2.f I think a lot of these need some example code (before and after). > What is a statement modifier? &&, !, etc? > No, a statement modifier is like this: puts "Do only if I'm true!" if me == true Examples will be provided to clear up any confusion. > 2.h.i There is no advantage to using single quotes or double quotes > other than safety > --> Double quotes allow interpolation and have more escape sequences. > What is "safety" in this context? > "Advantage" should say "speed advantage"; in other languages (i.e., PHP) there is a different in how these things are handled, thus affecting speed during certain operations. I'm not sure why I put safety there; probably something to do with escape sequences. Ill have to look back at my more annotated version and clear that up. > 2.h.ii Don���t combine string literals and variables using +; rather, > use interpolation > --> What if I want to append something to an existing string? Should I > use existingString = "#{existingString}#{newString}" or existingString > << newString? (Or +=?) > Probably, yes, any of those. I'm mostly referring to not doing something like this: mystring = "This is what " + your_name + " is looking for in " + that_place That's how I did strings when I first came to Ruby, but it's not very pretty; it's just my PHP roots showing. Plus, using interpolation gives you an automatic to_s. Thanks for the feedback all! I'm working on some revisions and such right now, but keep it coming. :) --Jeremy -- My free Ruby e-book: http://www.humblelittlerubybook.com/book/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.com/