Jared Grippe

Javascript like Ruby

After reading Yehuda Katz’s post Metaprogramming in Ruby: It’s All About the Self and I’ve realized you can do some funky javascript like stuff in ruby.

For Example you can define methods that have access to a javascript kind of “private” variable:

http://gist.github.com/236266

You could use this technique to simulate module variables like so:

http://gist.github.com/236430

You can even apply a method onto another object just like Javascript’s Function#apply

http://gist.github.com/236464

The difference being in Ruby you can only bind (and therefore call) a method on an instance of the object it was defined on. For instance if you try and bind Hash#keys onto an array instance you’ll get the `TypeError: bind argument must be an instance of Hash`


  1. jaredgrippe posted this
Comments
blog comments powered by Disqus
To Tumblr, Love Metalab