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.constructor.getCount();


#Ruby
p = Person.new
p.class.count

3. Define method of an object

//JavaScript
var p = new Person();
p.hello = function() {
alert('hello');
};


//Ruby
p = Person.new
def p.hello
puts "hello"
end

4. Define class methods

//JavaScript
Person.hello = function() {
alert('hello');
};


//Ruby
def Person.hello
puts "hello"
end

22 comments:

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.

Anonymous 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

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

Ancy merina said...
This comment has been removed by the author.
Raj Kumar said...
This comment has been removed by the author.
Bhavani Propladder said...

Nice Blog and thanks for sharing with us,

PVR Anmol
PVR Maha


Jyothika Propladder said...

Nice blog, and thanks for sharing the post.

PVR Anmol
PVR Maha
Indis Viva City
Prestige High Fields
Pbel City

maxidorhomes said...

Nice blog thanks for sharing with us.
Expandable Security Doors

Jyothika Propladder said...

Nice Blog,I feel like all your ideas are incredible! Great job!!!
I have some information about:

Prestige High Fields
Brigade-Citadel
Pbel City

Prestige Tranquil said...
This comment has been removed by the author.
Maxidor said...

Great Blog, Thanks for sharing...

In South Africa, adding Custom Window Security Burglar-Bars in your home or office can be quite a challenging task. The Maxi-Grill Burglar Bars are designed to fit neatly inside the space of any regular window or opening.Our unique tamper-proof fastening design ensures the more you tamper with our product, the better it protects.

Burglar Bars

Roller-Shutters said...

Good Information, Thanks for Sharing....

Maxidor Offering a Beautiful Roller Shutter, Garage Shutter and Doors for you Home and Office. Two Way Security Process stronger and Smart Lock System. Maxidor

Roller Shutters


Bhavani Propladder said...

Nice...I really like your article...I think that you spent more time and effort on your blog...thanks for sharing this good information & i have some information about the Real Estate luxurious projects in Hyderabad, Telangana.
This Project will helpful for those who are looking to buy their Dream Home with low budget..
Thanks for Sharing..!.!

Aditya Athena Hyderabad
The Botanika
Sumadhura Horizon
Spring Is In The Air
Ambience Courtyard Hyderabad



Urbanrise Talk said...

Nice article thanks for sharing with us...& I have some information about Urbanrise Talk of Hyderabad is a new project located in Bachupally, Miyapur. It provides 2,3 & 4 BHK Luxury residences available for sale in affordable prices.

Urbanrise Talk Of Hyderabad
Urbanrise Talk Of Hyderabad Bachupally
Urbanrise Talk Of Hyderabad Miyapur

Ramky One Galaxia said...

Nice blog,Thanks for sharing with us.
Ramky One Galaxia
Ramky One Galaxia Phase 2
Ramky One Galaxia Nallagandla
Ramky One Galaxia Gachibowli
Ramky One Galaxia Hyderabad

rahul said...

Nice blog, Informative. Thanks for sharing
Ambience Courtyard
Prestige Tranquil
Brigade Citadel Hyderabad
Sumadhura Olympus
SAS Crown Hyderabad
Vaishnavi Houdini
kalpataru avante
Soul of Twin Cities
provident kenworth

rahul said...

Nice blog, Informative. Thanks for sharing
Urbanrise on cloud 33
Urbanrise Codename Talk Of Hyderabad
Urbanrise Talk Of Hyderabad

rahul said...

Nice blog, Informative. Thanks for sharing
Urbanrise on cloud 33

SPORTS, TOYS, CRAFTS & BABY PRODUCTS said...

All the furniture you want to furnish your home affordable prices. Unlimited furniture for your 2 BHK at a fraction of the cost of buying.

Best HOME FURNISHING Online

Anusha Propladder said...

Real Estate, Property Portal in Bangalore and Hyderabad Helping customers on which property best suits their individual and family needs.
Location is in Hyderabd
Plot No: 56, Flat No: 201,
Siddhartha Residency, Kavuri Hills,
Beside Jubilee Ridge Hotel, Madhapur, Hyderabad- 500033.
Phone No: 040-65300003,65400004.

snr the elite said...

Nice Blog, I feel like all your ideas are incredible! Great job!!! Aim also share some information with you about SNR The Elite is developed by SNR Avenue, Located in Gopanpally, Hyderabad. SNR Avenues is India’s speedy developing assets Improvement Company. SNR Avenues' attention is on the improvement of top-rate residential & business properties, which deal with the necessities of our clientele. It provides 2 & 3 bhk luxury flats available for sale at affordable prices. Call +91-7070707098

Subscribe in a Reader