[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Class Methods and Instance Methods

arose

6/9/2006 5:06:00 PM

I'm reading Why's (Poingnant) Guid to Ruby chapter 3.

Class methods vs. Instance methods is mentioned.

This is what he says:

class methods are usually attached after variables and constants.
Rather than a dot, a double colon is used.
Door::new( :oak )

I talked some in this group about a little program I wrote.
g=File.open(readfile,"r")
g.each_line {|readline| File.new((readline).chomp, "w")}

Now File is a class correct?

Why not File::new in the second line of code above?

2 Answers

Dave Burt

6/9/2006 5:31:00 PM

0

arose wrote:
> I'm reading Why's (Poingnant) Guid to Ruby chapter 3.
>
> Class methods vs. Instance methods is mentioned.
>
> This is what he says:
>
> class methods are usually attached after variables and constants.
> Rather than a dot, a double colon is used.
> Door::new( :oak )
>
> I talked some in this group about a little program I wrote.
> g=File.open(readfile,"r")
> g.each_line {|readline| File.new((readline).chomp, "w")}
>
> Now File is a class correct?
>
> Why not File::new in the second line of code above?

Both are allowed. They're two ways of saying the same thing. You can
also use the double-colon to call instance methods:

"foo"::length

Now, the reason for this is simple. Class methods ARE instance methods,
because classes themselves are objects.

By convention, the double-colon seems generally reserved for class
methods. I've never seen it used other than after a capitalised class
constant: Foo::bar.

A lot of people use the dot always, though. I think that keeps things
clearer; I see no reason to use a second operator when the first does
the job.

Cheers,
Dave

arose

6/9/2006 7:33:00 PM

0

Exactly what I was looking for, thanks.



Dave Burt wrote:
> arose wrote:
> > I'm reading Why's (Poingnant) Guid to Ruby chapter 3.
> >
> > Class methods vs. Instance methods is mentioned.
> >
> > This is what he says:
> >
> > class methods are usually attached after variables and constants.
> > Rather than a dot, a double colon is used.
> > Door::new( :oak )
> >
> > I talked some in this group about a little program I wrote.
> > g=File.open(readfile,"r")
> > g.each_line {|readline| File.new((readline).chomp, "w")}
> >
> > Now File is a class correct?
> >
> > Why not File::new in the second line of code above?
>
> Both are allowed. They're two ways of saying the same thing. You can
> also use the double-colon to call instance methods:
>
> "foo"::length
>
> Now, the reason for this is simple. Class methods ARE instance methods,
> because classes themselves are objects.
>
> By convention, the double-colon seems generally reserved for class
> methods. I've never seen it used other than after a capitalised class
> constant: Foo::bar.
>
> A lot of people use the dot always, though. I think that keeps things
> clearer; I see no reason to use a second operator when the first does
> the job.
>
> Cheers,
> Dave