[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Basic ruby Question

Deepak Gole

7/4/2008 5:07:00 AM

[Note: parts of this message were removed to make it a legal post.]

Hello,

I didn't understand the following concept.

*class Universe

private
def self.private_method
p "=========Hello world======="
end

end

Universe.**private_method** => "=========Hello world======="
*

How come I get the o/p ( *"=========Hello world=======" * ) as I have
declared that method as private.

Thanks in advance
Deepak Gole (DG)

4 Answers

Raveendran Jazzez

7/4/2008 5:44:00 AM

0

Hi All,

But it was working in this way.

class Universe
def self.private_method
p "=========Hello world======="
end
end
u=Universe.new
u.private_method

# => private.rb:7: undefined method `private_method' for
#<Universe:0x2b66aa0> (NoMethodError)

what is the Difference betweeen these two types?

u=Universe.new
u.private_method && Universe.private_method


Regards,
P.Raveendran
http://raveendran.wor...
--
Posted via http://www.ruby-....

Torsten Mangner

7/4/2008 6:00:00 AM

0

Raveendran Jazzez wrote:

> But it was working in this way.
>
> class Universe
> def self.private_method
> p "=========Hello world======="
> end
> end
> u=Universe.new
> u.private_method
>
> # => private.rb:7: undefined method `private_method' for
> #<Universe:0x2b66aa0> (NoMethodError)

Your second example, does not work either. You tried to call a
class-method on an object.

I don't know how useful a private class-method could be. it doesn't make
to much sense for me.

i'm pretty sure, all you want is a simple instance-method: leave out the
"self." at the methods declaration:

private
def private_method
p "something"
end

> what is the Difference betweeen these two types?
>
> u=Universe.new
> u.private_method && Universe.private_method

for the difference between instance and class-methods use google on
there terms. the first explanation i found, should be sufficient:
http://www.juixe.com/techknow/index.php/2007/01/22/ruby-class...
--
Posted via http://www.ruby-....

Stefano Crocco

7/4/2008 7:07:00 AM

0

On Friday 04 July 2008, Deepak Gole wrote:
> Hello,
>
> I didn't understand the following concept.
>
> *class Universe
>
> private
> def self.private_method
> p "=========Hello world======="
> end
>
> end
>
> Universe.**private_method** => "=========Hello world======="
> *
>
> How come I get the o/p ( *"=========Hello world=======" * ) as I have
> declared that method as private.
>
> Thanks in advance
> Deepak Gole (DG)

The private method only makes instance methods private, not class methods.
This means the reason you can call Universe.private_method is that, despite
its name, the method is still public. To make a class method private, you can
use the Module#private_class_method method:

class Universe

def self.private_method
p "Hello world"
end

private_class_method :private_method

end

Universe.private_method

=> private method `private_method' called for Universe:Class (NoMethodError)

I hope this helps

Stefano



Deepak Gole

7/7/2008 4:47:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hello stefano,

Thanks for your explanation.


On Fri, Jul 4, 2008 at 12:37 PM, Stefano Crocco <stefano.crocco@alice.it>
wrote:

> On Friday 04 July 2008, Deepak Gole wrote:
> > Hello,
> >
> > I didn't understand the following concept.
> >
> > *class Universe
> >
> > private
> > def self.private_method
> > p "=========Hello world======="
> > end
> >
> > end
> >
> > Universe.**private_method** => "=========Hello world======="
> > *
> >
> > How come I get the o/p ( *"=========Hello world=======" * ) as I have
> > declared that method as private.
> >
> > Thanks in advance
> > Deepak Gole (DG)
>
> The private method only makes instance methods private, not class methods.
> This means the reason you can call Universe.private_method is that, despite
> its name, the method is still public. To make a class method private, you
> can
> use the Module#private_class_method method:
>
> class Universe
>
> def self.private_method
> p "Hello world"
> end
>
> private_class_method :private_method
>
> end
>
> Universe.private_method
>
> => private method `private_method' called for Universe:Class
> (NoMethodError)
>
> I hope this helps
>
> Stefano
>
>
>
>