Created
February 17, 2012 22:19
-
-
Save michaelfeathers/1855765 to your computer and use it in GitHub Desktop.
Five lines that turn Ruby into a different language
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 Object | |
def method_missing m, *args | |
Object.respond_to?(m, true) ? Object.send(m, self, *args) : super | |
end | |
end |
No, object structure change does not change the language.
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.
I would use the term 'constrained' not 'different' for a DSL, but that's
just me.
On 2/17/2012 11:02 PM, michaelfeathers wrote:
solojavier: It allows you to chain functions onto arbitrary objects. Each function that you use in this style is understood to accept the self of the previous link in the chain as its first argument. I know that is abstract, but here is a code example:
def plus_one(thing)
thing.map {|x| x + 1 }
end
[1,2,3].plus_one.plus_one == [3,4,5]
ArrayMac: A language is a set of conventions around expression. The term DSL (Domain-Specific Language) uses the word 'language' in this sense.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1855765
##
…---
|\/| Randy A MacDonald | If the string is too tight, it will snap
|\| arraymac@ns.sympatico.ca| If it is too loose, it won't play...
BSc(Math) UNBF '83 | APL: If you can say it, it's done.
Natural Born APL'er | I use Real J
Experimental webserver http://mormac.homeftp.net/
------------------------------------------------<-NTP>----{ gnat }-
Thanks for the response.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is this about? Newbie in ruby