Skip to content

Instantly share code, notes, and snippets.

@karthiks
Forked from michaelfeathers/gist:1855765
Created February 18, 2012 08:03
Show Gist options
  • Save karthiks/1858132 to your computer and use it in GitHub Desktop.
Save karthiks/1858132 to your computer and use it in GitHub Desktop.
Five lines that turn Ruby into a different language
class Object
def method_missing m, *args
Object.respond_to?(m, true) ? Object.send(m, self, *args) : super
end
end
@karthiks
Copy link
Author

Question by @solojavier: What is this about? Newbie in ruby. (I had the same question, in my mind! - A Ruby Enthusiast)

Explanation given by @michaelfeathers: It allows you to chain functions onto arbitrary objects. Each function that you use in this style is understood to accept the return value of the previous link in the chain as its first argument. I know that is abstract, but here is a code example:

def plus array, n
  array.map { |x| x + n }
end

[1,2,3].plus(1).plus(2) == [4,5,6]

ArrayMac: A language is a set of conventions around expression. The term DSL (Domain-Specific Language) uses the word 'language' in this sense.

@karthiks
Copy link
Author

@michaelfeathers tweeted the gist as "Five lines that turn Ruby into a different language". I guess, by language he meant DSL.

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