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 Patra <lists@ruby-forum.com> wrote:
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?
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
···
On 28.02.2013 12:34, Tukai Patra wrote:
class Foo
def show
p "hi"
end
=> nil
class Foo
alias :old_show :show
def show
p "hi! I am there."
old_show
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?
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
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` ?
@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.
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
You obviously didn't even try to just call the #old_show method.
Arup Rakshit, Love U Ruby, Xavier R., Tukai Patra:
When you think we are so stupid that we do not recognize you in spite
of your new user names then you shouldn't ask us for help.
···
Am 28.02.2013 13:34, schrieb Tukai Patra:
class Foo
def show
p "hi"
end
=> nil
class Foo
alias :old_show :show
def show
p "hi! I am there."
old_show
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 Patra <lists@ruby-forum.com> > 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`?
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?
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
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
···
On 2/28/13 9:54 AM, Hans Mackowiak wrote:
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.
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:
1. With alias you copy a method.
2. You can achieve the same with alias_method.
3. You can achieve something similar by doing def new_meth(*a,&b)
old_meth(*a,&b) end.
4. When defining a method all previous definitions under that name are gone.
5. Option 2 and 1 actually differ from 3 if you redefine old_meth
(exercise for the user).
6. What constitutes dangerous depends on the expectations and the
desired behavior.
7. 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
···
On Thu, Feb 28, 2013 at 4:50 PM, Tukai Patra <lists@ruby-forum.com> wrote:
You obviously didn't even try to just call the #old_show method.
Arup Rakshit, Love U Ruby, Xavier R., Tukai Patra:
When you think we are so stupid that we do not recognize you in spite
of your new user names then you shouldn't ask us for help.
···
---------------------------------------------------------------------------------------------
From Trolls with multiple handles:
I am now convinced (as many of you are aware) there are various trolls with multiple handles whose sole purpose is to infiltrate well meaning threads with obfuscation and red herrings and with the intent to dummy down and discredit whatever topic is being discussed.
Now, these are not your typical die-hard skeptics or die-hard believers. These are people who intentionally try to railroad threads with nonsensical arguments or off-topic rants.
You can usually spot them due to their activity level, stars, flag counts, etc.
----------------------------------------------------------------------------------------------
Far too many of the questions posed are easily found . "Google is your friend". And too many meaningful answers are ignored. Even our maxim MINSWAN can be pushed to it's limits.
Lets go with 'YAATSGFY' and use this as the sole content of all replies to questions and comments posed by this troll.
Maybe he (or she) will start to get the message...