Alias problem

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

On Dec 1, 4:41 am, Pokkai D. [email protected] 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 :slight_smile:

Gavin K. wrote:

On Dec 1, 4:41 am, Pokkai D. [email protected] 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)

On Dec 2, 2007, at 12:20 AM, Pokkai D. 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.

class Cls1 def view puts "from view" end end

c1 = Cls1.new
c1.view

class Cls1
private :view
end

c1.view

from view NoMethodError: private method ‘view’ called for #<Cls1:0x80e10at top level in untitled document at line 14

Regards, Morton

Morton G. wrote:

Regards, Morton

thats what i want .

thank you

Morton G. wrote:

On Dec 2, 2007, at 12:20 AM, Pokkai D. 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.

class Cls1 def view puts "from view" end end

c1 = Cls1.new
c1.view

class Cls1
private :view
end

c1.view

from view NoMethodError: private method �view� called for #<Cls1:0x80e10at top level in untitled document at line 14

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 ?

Pokkai D. 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

Morton G. wrote:

On Dec 2, 2007, at 12:20 AM, Pokkai D. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs