alias problem

P

Pokkai Dokkai

is there any idea to change public definition to private definition by
using alias or some others....
 
P

Phrogz

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

Pokkai Dokkai

Gavin said:
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)
 
M

Morton Goldberg

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

Pokkai Dokkai

Morton said:
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 ?
 
S

Sebastian Hungerecker

Pokkai said:
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.
TypeError: can't modify frozen class

See?

HTH,
Sebastian
 
D

Drew Olson

Morton said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top