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

Ancy merina said...

I like your post, really interesting facts and information.
Website Development services in Bangalore
Web Design Service in Bangalore
Top SEO Companies in Bangalore

نور الهدى said...

شركة نقل عفش
شركة شحن من الدمام الى مصر
شركة ديكورات بالرياض
شركة دهانات بالرياض
شركة شحن من الرياض الى مصر

Raj Kumar said...
This comment has been removed by the author.
Unique Residency said...

Nice blog and thanks for sharing the post.

Unique Residency
Unique Residency Manikonda
Unique Residency Hyderabad

5689 said...

off white clothing
reebok outlet
red bottom shoes
christian louboutin outlet
champion clothing
off white shoes
christian louboutin
ugg boots

Yaro Gabriel said...

ray ban sunglasses
hugo boss
ralph lauren polo
michael kors outlet
coach outlet
prada handbags
coach outlet
michael kors outlet
moncler outlet
nike blazers

Subscribe in a Reader