[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

alias problem

Pokkai Dokkai

12/1/2007 11:41:00 AM

is there any idea to change public definition to private definition by
using alias or some others....
--
Posted via http://www.ruby-....

7 Answers

Phrogz

12/1/2007 2:21:00 PM

0

On Dec 1, 4:41 am, Pokkai Dokkai <bad_good_l...@yahoo.com> wrote:
> is there any idea to change public definition to private definition by
> using alias or some others....

Slim2:~ phrogz$ qri private
---------------------------------------------------------
Module#private
private => self
private(symbol, ...) => self
------------------------------------------------------------------------
With no arguments, sets the default visibility for subsequently
defined methods to private. With arguments, sets the named
methods
to have private visibility.

module Mod
def a() end
def b() end
private
def c() end
private :a
end
Mod.private_instance_methods #=> ["a", "c"]

Please RTFM in the future :)


Pokkai Dokkai

12/2/2007 5:20:00 AM

0

Gavin Kistner wrote:
> On Dec 1, 4:41 am, Pokkai Dokkai <bad_good_l...@yahoo.com> wrote:
> Slim2:~ phrogz$ ri private

hey please understand my question correctly
that is ,
change public definition(public method) to private definition(private
method) by
using some ideas....

i want like this (ofcourse below is wrong)
-------------------------------------------------
class Cls1
def view
puts "from view"
end
def self.pp
alias :private view :public view
end
end
c1=Cls1.new
c1.view ----->from view
Cls1.pp
c1.view ----->undefined method `view' for #<Cls1:0xb7daa8b0>
(NoMethodError)
---------------------------------------------------
--
Posted via http://www.ruby-....

Morton Goldberg

12/2/2007 12:35:00 PM

0


On Dec 2, 2007, at 12:20 AM, Pokkai Dokkai wrote:

> Gavin Kistner wrote:
>> On Dec 1, 4:41 am, Pokkai Dokkai <bad_good_l...@yahoo.com> wrote:
>> Slim2:~ phrogz$ ri private
>
> hey please understand my question correctly
> that is ,
> change public definition(public method) to private definition(private
> method) by
> using some ideas....
>
> i want like this (ofcourse below is wrong)
> -------------------------------------------------
> class Cls1
> def view
> puts "from view"
> end
> def self.pp
> alias :private view :public view
> end
> end
> c1=3DCls1.new
> c1.view ----->from view
> Cls1.pp
> c1.view ----->undefined method `view' for #<Cls1:0xb7daa8b0>
> (NoMethodError)


I don't believe you can do what I think you want with 'alias'. Maybe =20
the following will work for you.

<code>
class Cls1
def view
puts "from view"
end
end

c1 =3D Cls1.new
c1.view

class Cls1
private :view
end

c1.view
</code>

<result>
from view
NoMethodError: private method =91view=92 called for #<Cls1:0x80e10at top =
=20
level
in untitled document at line 14
</result>

Regards, Morton=

Pokkai Dokkai

12/2/2007 2:39:00 PM

0

Morton Goldberg wrote:

> Regards, Morton

thats what i want .

thank you
--
Posted via http://www.ruby-....

Pokkai Dokkai

12/2/2007 2:43:00 PM

0

Morton Goldberg wrote:
> On Dec 2, 2007, at 12:20 AM, Pokkai Dokkai wrote:
>
>> i want like this (ofcourse below is wrong)
>> c1.view ----->from view
>> Cls1.pp
>> c1.view ----->undefined method `view' for #<Cls1:0xb7daa8b0>
>> (NoMethodError)
>
>
> I don't believe you can do what I think you want with 'alias'. Maybe
> the following will work for you.
>
> <code>
> class Cls1
> def view
> puts "from view"
> end
> end
>
> c1 = Cls1.new
> c1.view
>
> class Cls1
> private :view
> end
>
> c1.view
> </code>
>
> <result>
> from view
> NoMethodError: private method �view� called for #<Cls1:0x80e10at top
> level
> in untitled document at line 14
> </result>
>
> Regards, Morton


actualluy all ruby classes are not closed (we can add anything at any
time)
like as above

so how to close a ruby class ?

i thing freeze is used to protect modification for object.
like that what is used to protect modification for class ?

--
Posted via http://www.ruby-....

Sebastian Hungerecker

12/2/2007 2:49:00 PM

0

Pokkai Dokkai wrote:
> i thing freeze is used to protect modification for object.
> like that what is used to protect modification for class ?

Classes are objects, too. SomeClass.freeze works just fine.

>> String.freeze
=> String
>> class String; def bla() end end
TypeError: can't modify frozen class

See?

HTH,
Sebastian
--
NP: Porcupine Tree - Glass Arm Shattering
Jabber: sepp2k@jabber.org
ICQ: 205544826

Drew Olson

12/3/2007 8:46:00 PM

0

Morton Goldberg wrote:
> On Dec 2, 2007, at 12:20 AM, Pokkai Dokkai wrote:

You could also do something similar to to Morton's solution, but on the
metaclass. This would me that you would only make that method private
for the specific instance of the object you're currently working with.
You could also do it from an instance method (like in your example). Is
this what you were looking for?

class Stuff
def metaclass
class << self; self; end
end

def my_method
puts "hi from method!"
end

def pp
metaclass.instance_eval do
private :my_method
end
end
end

s = Stuff.new
s.my_method
s.pp
s.my_method
--
Posted via http://www.ruby-....