class Foo
def show
p “hi”
end
end
=> nil
class Foo
alias :old_show :show
def show
p “hi! I am there.”
old_show
end
end
=> nil
foo = Foo.new
=> #Foo:0x117bca0
foo.show
“hi! I am there.”
“hi”
=> “hi”
The above code is very fine. But looking for if possible to call that
old_show
method by the object of Foo
from IRB main
as we called
foo.show
? - Is this possible?
On Thu, Feb 28, 2013 at 1:34 PM, Tukai P. [email protected]
wrote:
old_show
The above code is very fine. But looking for if possible to call that
old_show
method by the object of Foo
from IRB main
as we called
foo.show
? - Is this possible?
You did call #old_show.
Cheers
robert
Robert K. wrote in post #1099527:
On Thu, Feb 28, 2013 at 1:34 PM, Tukai P. [email protected]
wrote:
old_show
The above code is very fine. But looking for if possible to call that
old_show
method by the object of Foo
from IRB main
as we called
foo.show
? - Is this possible?
You did call #old_show.
Cheers
robert
Yes, but that I did inside from the show
. I am telling if possible to
call the same old_show
without the help of show
?
On 28.02.2013 12:34, Tukai P. wrote:
old_show
The above code is very fine. But looking for if possible to call that
old_show
method by the object of Foo
from IRB main
as we called
foo.show
? - Is this possible?
Uh, yes you just call it, unless I totally misunderstand your question?
class Foo
def bar
puts “bar”
end
end
==>nil
class Foo
alias :baz :bar
def bar
puts “baz”
baz
end
end
==>nil
foo = Foo.new
==>#Foo:0x10bc11040
foo.bar
baz
bar
==>nil
foo.baz
bar
==>nil
Alex G. wrote in post #1099529:
On 28.02.2013 12:34, Tukai P. wrote:
old_show
Uh, yes you just call it, unless I totally misunderstand your question?
class Foo
def bar
puts “bar”
end
end
==>nil
class Foo
alias :baz :bar
def bar
puts “baz”
baz
end
end
in the above you put baz
inside bar
, thus you were able. I am
telling without putting it into any instance method, can we call it,from
the outside of the class?
Hope myself cleard now my intention.
On 28.02.2013 12:51, Tukai P. wrote:
end
in the above you put baz
inside bar
, thus you were able. I am
telling without putting it into any instance method, can we call
it,from
the outside of the class?
Hope myself cleard now my intention.
Surely the very last line of my session (which you neatly removed from
your quote) is calling it ‘from the outside of the class’?
how should ruby know that it should automatic old_show inside show?
how should it know if it should be called before or after your code or
between? what about the parameters?
that all questions ruby cant answer for you, so you need to call it
yourself when using alias
Alex G. wrote in post #1099529:
On 28.02.2013 12:34, Tukai P. wrote:
old_show
class Foo
def bar
puts “bar”
end
end
==>nil
class Foo
alias :baz :bar
def bar
puts “baz”
baz
end
end
==>nil
foo = Foo.new
==>#Foo:0x10bc11040
Yes the below one I was looking for if possible or not. Can you explain
how does it possible? what internal task ruby did for that call?
foo.baz
bar
==>nil
Sorry I overlooked it
On Thu, Feb 28, 2013 at 2:02 PM, Tukai P. [email protected]
wrote:
Alex G. wrote in post #1099529:
On 28.02.2013 12:34, Tukai P. wrote:
Yes the below one I was looking for if possible or not. Can you explain
how does it possible? what internal task ruby did for that call?
foo.baz
bar
==>nil
Sorry I overlooked it
The alias keywords creates a new instance method in the class, no
different (from the outside) than any other methods you define on it:
1.9.2p290 :001 > class Test
1.9.2p290 :002?> def m
1.9.2p290 :003?> end
1.9.2p290 :004?> end
=> nil
1.9.2p290 :006 > Test.instance_methods(false)
=> [:m]
1.9.2p290 :007 > class Test
1.9.2p290 :008?> alias :old_m :m
1.9.2p290 :009?> end
=> nil
1.9.2p290 :010 > Test.instance_methods(false)
=> [:m, :old_m]
Jesus.
class Foo
def show
p “hi”
end
end
#=> nil
class Foo
alias :old_show :show
def show
p “hi! I am there.”
old_show
end
end
#=> nil
foo = Foo.new
#=> #Foo:0x116bde8
foo.show
“hi! I am there.”
“hi”
#=> “hi”
foo.old_show
“hi”
#=> “hi”
Now say I did the below:
class Foo
def old_show
p “There is another with the same name as of mine”
end
end
foo.old_show
“There is another with the same name as of mine”
#=> “There is another with the same name as of mine”
And here foo.old_show
is confusing.And Ruby gives the priority to the
latest old_show
version. Now in such a
situation, - any more chance to call the aliased version of old_show
?
How many user accounts does the same person need?
Humm! That means in a large code-base, alias
might be dangerous,where
if someone do the same what I did above.
Now my question is - is there any replacement of such alias
which can
do the same what alias
does above?
any replacement of the below functionality which alias does with risk
incurred with it.
class Foo
def show
p “hi”
end
end
=> nil
class Foo
alias :old_show :show
def show
p “hi! I am there.”
old_show
end
end
=> nil
foo = Foo.new
=> #Foo:0x117bca0
foo.show
“hi! I am there.”
“hi”
=> “hi”
Tukai P. wrote in post #1099551:
And here foo.old_show
is confusing.And Ruby gives the priority to the
latest old_show
version. Now in such a
situation, - any more chance to call the aliased version of old_show
?
you itself does “overwrite” the method, you dont get it back, the old
version is GONE
and its not confusing it is clear if you try to think
alias is only dangerous because you do not understand it
there is no other build in way
and as i understand YOUR problem is not the alias, its the overwrite of
the aliased method above!
and its not confusing, your code is only annoying
as i said above,
- you cant made that the orginal method is automatic called (your first
question)
- you cant prevent that the alias method will be overwritten (your
second question)
that is not how ruby/alias works … live with that!
and ignoring other users that wants to help you is not a nice way in
this forum
@Hans - Yes I do understand how alias
works. Might be you didn’t catch
my aim. I have a hope anyone out there might understood my intention.
Let’s wait you can see how the same could be done with other way’s,if
someone answered it. Meanwhile If I get any solution,I will present here
with explanation.
Don’t worry.
Tukai/Xavier/Love U Ruby/Arup,
Go away and don’t come back until you have spent some time actually
learning the basics of Ruby. Also, don’t come back until you learn to be
respectful to people helping you. The fact that you keep making new
usernames is not only annoying but it’s just pointing out that even
you know you are annoying the piss out of others on this mailing list.
-Ryan
Ryan, you’re considerably more annoying than him.
Please stop. Take a walk in the part, enjoy the nature, drop the
bullshit posts.
How many user accounts does the same person need?
Yes, this guy here sounds like ILoveRuby … :\
That means in a large code-base, alias
might be dangerous
where if someone do the same what I did above.
I use alias all the time.
Ruby-gnome uses aliases all over the place.
Can you believe this?
It does not seem dangerous at all, it simply works.
Now my question is - is there any replacement of
such alias
which can do the same what alias
does above?
Do you speak the english language?
You basically asked:
“is there a replacement for alias that can do
what alias can do”
Yes. The name is:
alias
There is also alias_method but since you ask fake
questions anyway, I am sure you’ll ignore that.
any replacement of the below functionality
which alias does with risk incurred with it.
There is no risk.
I think you need to use PHP, please improve PHP
and not try to “improve” ruby.
And, by the way, if you were serious, you would
go over to bugs ruby-lang and file feature
requests.
On Thu, Feb 28, 2013 at 4:50 PM, Tukai P. [email protected]
wrote:
@Hans - Yes I do understand how alias
works. Might be you didn’t catch
my aim. I have a hope anyone out there might understood my intention.
I hope you do.
Let’s wait you can see how the same could be done with other way’s,if
someone answered it. Meanwhile If I get any solution,I will present here
with explanation.
Facts:
- With alias you copy a method.
- You can achieve the same with alias_method.
- You can achieve something similar by doing def new_meth(*a,&b)
old_meth(*a,&b) end.
- When defining a method all previous definitions under that name are
gone.
- Option 2 and 1 actually differ from 3 if you redefine old_meth
(exercise for the user).
- What constitutes dangerous depends on the expectations and the
desired behavior.
- Invoking a method from inside or outside an object (meaning self
pointing to the instance to invoke the method on or not) only matters
for private methods.
irb(main):001:0> class Foo
irb(main):002:1> def x; 1; end
irb(main):003:1> alias_method :y, :x
irb(main):004:1> end
=> Foo
irb(main):005:0> Foo.new.y
=> 1
irb(main):006:0> class Foo
irb(main):007:1> def x; 2; end
irb(main):008:1> end
=> nil
irb(main):009:0> Foo.new.y
=> 1
irb(main):010:0> Foo.new.x
=> 2
Cheers
robert