Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created February 17, 2012 22:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelfeathers/1855765 to your computer and use it in GitHub Desktop.
Save michaelfeathers/1855765 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
@solojavier
Copy link

What is this about? Newbie in ruby

@ArrayMac
Copy link

No, object structure change does not change the language.

@michaelfeathers
Copy link
Author

solojavier: 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.

@ArrayMac
Copy link

ArrayMac commented Feb 18, 2012 via email

@solojavier
Copy link

Thanks for the response.

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