Skip to content

Instantly share code, notes, and snippets.

@wnuqui
Created May 10, 2012 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnuqui/2651356 to your computer and use it in GitHub Desktop.
Save wnuqui/2651356 to your computer and use it in GitHub Desktop.
method chain
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
@dominicpacquing
Copy link

nice code!

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