Favorite Ruby idioms, shortcuts?

S

Serg Koren

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.
 
P

Phrogz

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=cach...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 )
 
S

Serg Koren

Thanks that makes sense.


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 )
 
J

Jeremy McAnally

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

Thanks that makes sense.



--
http://www.jeremymcanally.com/

My books:
Ruby in Practice
http://www.manning.com/mcanally/

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top