Friday, February 19, 2010

Meta-programming in Ruby and JavaScript

Recently, I have been working with writing a ruby gem, Yoolk API Gem. What is really interesting for me is I do some meta programming and object-oriented programming in Ruby which I have never experienced before. A few month later, there is a requirement that my team needs to write in JavaScript, but I don't want to touch JavaScript really much. Therefore, my team member took over this task. Whenever I write code in Ruby, I just try to think how to do it in JavaScript as well. Several things that came up to my mind with some from my team member:

1. Defer class from a variable.

// JavaScript
var klass = "Person";
p = new window[klass]; //class without namespace
p = new yoolk[klass]; //class with namespace

# Ruby
klass = "Person"
p = Object.const_get(klass).new #class without namespace
p = Yoolk.const_get(klass).new #class with namespace

2. Access class method from instance object

// JavaScript
var p = new Person();

p =

3. Define method of an object

var p = new Person();
p.hello = function() {

p =
def p.hello
puts "hello"

4. Define class methods

Person.hello = function() {

def Person.hello
puts "hello"


Anonymous said...

Good fill someone in on and this enter helped me alot in my college assignement. Say thank you you on your information.

Anonymous said...

Correctly your article helped me terribly much in my college assignment. Hats afar to you enter, will look progressive for more interdependent articles soon as its united of my favourite issue to read.

fundoo said...

Thanks for the information, we will add this story to our blog, as we have a audience in this sector that loves reading like this” web development

tbcreations said...

You made some good points there. I did a search on the topic and found most people will agree with your blog.
Web Design Rochester NY
Web Designing Rochester NY
Web Development Rochester NY

sahana chinu said...

This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things.
Web development Company in India Company | Web development Company Bangalore
Web Development Company in Bangalore | Responsive Web development

Srinu said...

Nice blog and thanks for sharing the post. Check out some latest projects.

Lodha Meridian
SMR Vinay Iconia
Prestige Ivy League
Western Exotica
NCC Urban Gardenia
Quiescent Heights
NCC Urban One
NorthStar Homes District1

Anusha Rao said...

Great Blog

Godrej Air Hoodi
Arvind Skylands
Salarpuria Sattva Divinity
Assetz 63 Degree East
Brigade Buena Vista
Prestige Falcon City
Salarpuria Sattva East Crest
Brigade Lakefront

Subscribe in a Reader