# Pop Quiz: aliasing versus wrapping

**URL:** <https://www.ruby-forum.com/t/pop-quiz-aliasing-versus-wrapping/186067>\
**Category:** Ruby\
**Created:** [March 31, 2010, 6:33am UTC](https://www.ruby-forum.com/t/pop-quiz-aliasing-versus-wrapping/186067 "2010-03-31T06:33:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 31, 2010, 6:33am UTC](https://www.ruby-forum.com/t/pop-quiz-aliasing-versus-wrapping/186067/1 "2010-03-31T06:33:11Z")

</div>

Below are two files that differ only in the 2nd line of code in each.  
Can you guess what difference they will reveal? I must admit I was  
surprised when I ran into this.

$ cat alias.rb  
class Object  
alias\_method :to\_js, :inspect  
end

class Array  
def to\_js; “[#{map{|o|o.to\_js}.join(’,’)}]”; end  
end

class Hash  
def to\_js  
“{#{map{|k,v| “#{k.to\_s.to\_js}:#{v.to\_js}” }.join(’,’)}}”  
end  
end

[1, [true], {a:“foo”} ].each{ |o| puts o.to\_js }

$ cat wrapped.rb  
class Object  
def to\_js; inspect; end  
end

class Array  
def to\_js; “[#{map{|o|o.to\_js}.join(’,’)}]”; end  
end

class Hash  
def to\_js  
“{#{map{|k,v| “#{k.to\_s.to\_js}:#{v.to\_js}” }.join(’,’)}}”  
end  
end

[1, [true], {a:“foo”} ].each{ |o| puts o.to\_js }

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 31, 2010, 11:20am UTC](https://www.ruby-forum.com/t/pop-quiz-aliasing-versus-wrapping/186067/2 "2010-03-31T11:20:35Z")

</div>

2010/3/31 Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Below are two files that differ only in the 2nd line of code in each.  
> Can you guess what difference they will reveal? I must admit I was  
> surprised when I ran into this.
> 
> $ cat alias.rb  
> class Object  
> alias\_method :to\_js, :inspect  
> end

This one always invokes Object#inspect - unless you override #to\_js  
explicitly.

> $ cat wrapped.rb  
> class Object  
> def to\_js; inspect; end  
> end

This is the one you really want IMHO: #to\_js delegates to whatever  
#inspect is present in the class.

Pretty straightforward inheritance behavior - although I’d concede  
that it’s subtle.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 31, 2010, 11:31am UTC](https://www.ruby-forum.com/t/pop-quiz-aliasing-versus-wrapping/186067/3 "2010-03-31T11:31:42Z")

</div>

On Wed, Mar 31, 2010 at 10:01 AM, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Below are two files that differ only in the 2nd line of code in each.  
> Can you guess what difference they will reveal? I must admit I was  
> surprised when I ran into this.

It’s pretty clear if you think of it in terms of message handlers and  
method objects

> class Object  
> alias\_method :to\_js, :inspect  
> end

This binds the message handler Object#to\_js to the method object  
Object#inspect. This should be illustrative:

$ cat test.rb  
class Object  
alias\_method :to\_js, :inspect  
alias\_method :inspect, :hash  
end

a = Object.new  
puts a.inspect  
p a  
puts a.to\_js

$ ruby test.rb  
67266180  
67266180  
#Object:0x804cd08

> $ cat wrapped.rb  
> class Object  
> def to\_js; inspect; end  
> end

This creates a new method object, which contains a thunk that sends  
the message “inspect” to self when it is invoked, and binds the  
Object#to\_js message handler to that method object.

martin

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 31, 2010, 7:37pm UTC](https://www.ruby-forum.com/t/pop-quiz-aliasing-versus-wrapping/186067/4 "2010-03-31T19:37:05Z")

</div>

On Mar 31, 3:19 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This one always invokes Object#inspect - unless you override #to\_js explicitly.

Yes; specifically, in this case, Object#inspect on a String calls  
#to\_s (because it’s defined), so:  
puts “foo”.to\_js  
#=\> foo  
instead of the desired “foo” (with quotes).
