P
Phrogz
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 }
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 }