Favorite Ruby idioms, shortcuts?

Hi

As relatively new to Ruby, I’d be interested in seeing what people
think are the best idioms for Ruby. I’m trying to get used to doing
things the “Ruby way” (no pun intended). I’m interested in what you
think are your favorite ways of doing things in Ruby. Please include
a short description or comments. If I get enough of them I’ll
consolidate them and post them online.

Thanks for all your help and time.

PS - Anyone have a good explanation of why RDoc translates initialize
methods as ‘new’? Yes I know you instantiate via: X.new but why
does the method ‘initialize’ get documented as method ‘new’? Seems
a bit confusing for us newbies.

On Feb 15, 4:36 pm, Serg K. [email protected] wrote:

As relatively new to Ruby, I’d be interested in seeing what people
think are the best idioms for Ruby. I’m trying to get used to doing
things the “Ruby way” (no pun intended). I’m interested in what you
think are your favorite ways of doing things in Ruby. Please include
a short description or comments. If I get enough of them I’ll
consolidate them and post them online.

http://209.85.173.104/search?q=cache:6FhFfBVa9lIJ:rubygarden.org/Ruby/page/show/RubyIdioms+ruby+idioms&hl=en&ct=clnk&cd=2&gl=us&client=safari

PS - Anyone have a good explanation of why RDoc translates initialize
methods as ‘new’? Yes I know you instantiate via: X.new but why
does the method ‘initialize’ get documented as method ‘new’? Seems
a bit confusing for us newbies.

I assume because initialize only exists to be called on instances
after the ‘new’ for the class has created them. I have personally
never called #initialize directly; it’s always through new.

I suppose the idea is that by documenting the method that you actually
call, you know how to use the method. (Instead of causing new users to
think that they should write f = Foo.new; f.initialize( bar, 42 )

Thanks that makes sense.

Right. Your API documentation should typically document public API
over private, internal implementation details. Though it does seem
odd that somehow it doesn’t signify that the functionality documented
as new is actually initialize. I don’t know if there’s really a good
alternative to the current way it’s done, though.

–Jeremy

On Fri, Feb 15, 2008 at 7:13 PM, Serg K. [email protected] wrote:

does the method ‘initialize’ get documented as method ‘new’? Seems


http://www.jeremymcanally.com/

My books:
Ruby in Practice

My free Ruby e-book

My blogs:

http://www.rubyinpractice.com/