[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Favorite Ruby idioms, shortcuts?

Serg Koren

2/15/2008 11:37:00 PM

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.



4 Answers

Phrogz

2/16/2008 12:08:00 AM

0

On Feb 15, 4:36 pm, Serg Koren <sko...@comcast.net> 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&cli...

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

Serg Koren

2/16/2008 12:13:00 AM

0

>

Thanks that makes sense.



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


Phlip

2/16/2008 2:15:00 AM

0

Serg Koren wrote:

> As relatively new to Ruby, I'd be interested in seeing what people
> think are the best idioms for Ruby.

Coming to Ruby from C++, I learned to stop writing intricate balanced
classes, and start abusing blocks with reckless abandon.

This might be experience speaking here, but I have not yet found a way to
make a design worse by adding a block closure. If you are typing a
method, and you don't know what to type next, you just write 'yield', and
you let the calling method figure it out!

--
Phlip

Jeremy McAnally

2/16/2008 3:37:00 AM

0

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 Koren <skoren@comcast.net> wrote:
> >
>
> Thanks that makes sense.
>
>
>
> >
> >
> >> 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 )
> >
>
>
>



--
http://www.jeremymca...

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

My free Ruby e-book
http://www.humblelittlerub...

My blogs:
http://www.mrneigh...
http://www.rubyinpra...