Forum: Ruby Discussion on Ruby's `alias`

Posted by Tukai Patra (loveconcept)
on 2013-02-28 13:34
>> 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?
Posted by Robert Klemme (robert_k78)
on 2013-02-28 13:42
(Received via mailing list)
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
Posted by Tukai Patra (loveconcept)
on 2013-02-28 13:46
Robert Klemme wrote in post #1099527:
> 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`?
Posted by Alex Gutteridge (Guest)
on 2013-02-28 13:47
(Received via mailing list)
On 28.02.2013 12:34, Tukai Patra 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
Posted by Tukai Patra (loveconcept)
on 2013-02-28 13:51
Alex Gutteridge wrote in post #1099529:
> On 28.02.2013 12:34, Tukai Patra 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.
Posted by Hans Mackowiak (hanmac)
on 2013-02-28 13:55
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
Posted by Alex Gutteridge (Guest)
on 2013-02-28 13:56
(Received via mailing list)
On 28.02.2013 12:51, Tukai Patra 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'?
Posted by Tukai Patra (loveconcept)
on 2013-02-28 14:01
Alex Gutteridge wrote in post #1099529:
> On 28.02.2013 12:34, Tukai Patra 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 :)
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-02-28 14:59
(Received via mailing list)
On Thu, Feb 28, 2013 at 2:02 PM, Tukai Patra <lists@ruby-forum.com> 
wrote:
> Alex Gutteridge wrote in post #1099529:
>> On 28.02.2013 12:34, Tukai Patra 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.
Posted by Tukai Patra (loveconcept)
on 2013-02-28 15:31
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` ?
Posted by Joel Pearson (virtuoso)
on 2013-02-28 15:54
How many user accounts does the same person need?
Posted by Hans Mackowiak (hanmac)
on 2013-02-28 16:21
Tukai Patra 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
Posted by Tukai Patra (loveconcept)
on 2013-02-28 16:37
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"
>>
Posted by Hans Mackowiak (hanmac)
on 2013-02-28 16:43
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
Posted by Tukai Patra (loveconcept)
on 2013-02-28 16:48
@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. :)
Posted by Hans Mackowiak (hanmac)
on 2013-02-28 16:54
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
Posted by Marc Heiler (shevegen)
on 2013-02-28 16:55
> 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.
Posted by Ryan Victory (Guest)
on 2013-02-28 16:59
(Received via mailing list)
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
Posted by Bartosz Dziewoński (matmarex)
on 2013-02-28 17:23
(Received via mailing list)
Ryan, you're considerably more annoying than him.

Please stop. Take a walk in the part, enjoy the nature, drop the 
bullshit posts.
Posted by Robert Klemme (robert_k78)
on 2013-02-28 17:27
(Received via mailing list)
On Thu, Feb 28, 2013 at 4:50 PM, Tukai Patra <lists@ruby-forum.com> 
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:

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
Posted by Tukai Patra (loveconcept)
on 2013-02-28 18:28
See the below link:

http://blog.jayfields.com/2006/12/ruby-alias-metho...

And people who cursed me and tried repeatedly to be off-topic by
pointing me to the other users, to whom I am not familiarized with
anyway instead.

people here have very bad conception,they did have time to get busy with
bad discussions.

Still apologies if I disrespect one. But you did that with me.

anyway Thanks.
Posted by Tukai Patra (loveconcept)
on 2013-02-28 18:29
Robert Klemme wrote in post #1099574:
> On Thu, Feb 28, 2013 at 4:50 PM, Tukai Patra <lists@ruby-forum.com>
> 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.

Thanks Robert.
Posted by unknown (Guest)
on 2013-02-28 18:51
(Received via mailing list)
Am 28.02.2013 13:34, schrieb Tukai Patra:
>>> 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 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.
Posted by S.D (Guest)
on 2013-02-28 20:40
(Received via mailing list)
> 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.

http://www.abovetopsecret.com/forum/thread928096/pg1
---------------------------------------------------------------------------------------------
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.
Posted by Robert Klemme (robert_k78)
on 2013-03-01 07:59
(Received via mailing list)
On Thu, Feb 28, 2013 at 6:28 PM, Tukai Patra <lists@ruby-forum.com> 
wrote:
> See the below link:
>
> http://blog.jayfields.com/2006/12/ruby-alias-metho...

Avoiding exposure of the old method can also be achieved by making it 
private.

> And people who cursed me and tried repeatedly to be off-topic by
> pointing me to the other users, to whom I am not familiarized with
> anyway instead.
>
> people here have very bad conception,they did have time to get busy with
> bad discussions.

I'd be careful about making statements about people's perceptions.
After all, you get to know them only through *your* perception.
Usually it is much more successful to listen carefully to feedback and
take it as fact instead of claiming it's the others who do not see
reality. I don't have the time to go into more detail but the concept
of "reality" is actually quite a complex one and there are various
theories around that. I personally find this one pretty convincing:
http://en.wikipedia.org/wiki/Constructivist_epistemology

The interesting part is here:
http://en.wikipedia.org/wiki/Constructivist_episte...

Cheers

robert
Posted by Attila Gulyas (toraritte)
on 2013-03-01 08:44
(Received via mailing list)
Arup,

I have no idea what your point was but if you've simply tried it in
REPL you could've seen it yourself.
https://gist.github.com/toraritte/5063028

regards
Attila

On Fri, Mar 1, 2013 at 7:58 AM, Robert Klemme
Posted by Tukai Patra (loveconcept)
on 2013-03-01 09:53
Attila Gulyas wrote in post #1099675:
> Arup,
>
> I have no idea what your point was but if you've simply tried it in
> REPL you could've seen it yourself.
> https://gist.github.com/toraritte/5063028
>
> regards
> Attila
>
> On Fri, Mar 1, 2013 at 7:58 AM, Robert Klemme


Thanks for your github link. I explained my confusion above. And after 
that I found the link I shared. That link and your link both proved 
that,yes what I was looking for possible.

and please don't ask me as "Arup". My name is "Tukai".

Some people was barking there, let them bark. Might they had a dream 
about such, and typed here also. I don't care at their barks.

But thanks to you and Robert and Jesús to share valuable in-formations.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.