Skip to content

Instantly share code, notes, and snippets.

@lsegal
Created April 7, 2010 03:12
Show Gist options
  • Select an option

  • Save lsegal/358467 to your computer and use it in GitHub Desktop.

Select an option

Save lsegal/358467 to your computer and use it in GitHub Desktop.
Scoped implicit conversion in Ruby, code is http://github.com/lsegal/ruby/tree/implicit
class MyString < String
def foo; reverse + "X" end
end
module Foo
implicit String => MyString
class A
attr_reader :myattr
def initialize
@myattr = "hello"
end
end
end
p Foo::A.new.myattr.foo
p "hello".foo
# "ollehX"
# test.rb:17:in `<main>': undefined method `foo' for "hello":String (NoMethodError)
@phillmv

phillmv commented Apr 7, 2010

Copy link
Copy Markdown

That took me a minute to parse. Pretty cool.

@danielribeiro

Copy link
Copy Markdown

Great! Threads like this(http://news.ycombinator.com/item?id=1548670) show how much value this would add on Ruby's trunk. Hope it makes it there soon (on both 1.8.8 and 1.9.3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment