Created
May 10, 2012 06:16
-
-
Save wnuqui/2651356 to your computer and use it in GitHub Desktop.
method chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def b | |
data = [1, 2, 3] | |
def data.foo! | |
self.sample | |
end | |
data | |
end | |
end | |
A.new.b.foo! # => 2 | |
# -------------------------------------------------------- | |
# OR by 'extend' | |
# -------------------------------------------------------- | |
class A | |
def b | |
data = [1, 2, 3] | |
data.send :extend, InstanceMethods | |
data | |
end | |
end | |
module InstanceMethods | |
def foo! | |
self.sample | |
end | |
end | |
A.new.b.foo! # => 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice code!