CSC/ECE 517 Fall 2012/ch1b 1w57 mp: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
==1. Introduction==
==Introduction==


----
----
Line 5: Line 5:
One of the most impressive aspects of Ruby is its metaprogramming capabilities. As a dynamic language, Ruby gives you the freedom to define methods and even classes during runtime. Metaprogramming with Ruby, one can do in a few minutes what other languages may take hours to do. By cleverly planning your code and applying the techniques mentioned here, you’ll be able to write code that is DRYer, lighter, more intuitive and more scalable.
One of the most impressive aspects of Ruby is its metaprogramming capabilities. As a dynamic language, Ruby gives you the freedom to define methods and even classes during runtime. Metaprogramming with Ruby, one can do in a few minutes what other languages may take hours to do. By cleverly planning your code and applying the techniques mentioned here, you’ll be able to write code that is DRYer, lighter, more intuitive and more scalable.


==2. Metaprogramming in Ruby==
==Metaprogramming in Ruby==
 
----
----
 
===Open Classes===
===2.1 Open Classes===
 
----
----


Line 17: Line 14:
Comparing this to other object oriented languages like JAVA, we cannot modify the existing standard classes and add custom methods. We will have to create a new class of our own which inherits from the standard class and add new functionalities to it. Then, we need to use this user-defined class in place of the standard class.
Comparing this to other object oriented languages like JAVA, we cannot modify the existing standard classes and add custom methods. We will have to create a new class of our own which inherits from the standard class and add new functionalities to it. Then, we need to use this user-defined class in place of the standard class.
===2.2 Implementing custom methods in Open Classes===
===Implementing custom methods in Open Classes===


----
----
Line 37: Line 34:
Here, we define our own method 'euros' by extending the Numeric class. Euro-to-Dollar conversion can then be done by invoking the euros method as shown above.
Here, we define our own method 'euros' by extending the Numeric class. Euro-to-Dollar conversion can then be done by invoking the euros method as shown above.
===2.3 Method Missing===
===Method Missing===
----
----
When you send a message to an object, the object executes the first method it finds on its method lookup path with the same name as the message. If it fails to find any such method, it raises a NoMethodError exception - unless you have provided the object with a method called method_missing. The method_missing method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method.
When you send a message to an object, the object executes the first method it finds on its method lookup path with the same name as the message. If it fails to find any such method, it raises a NoMethodError exception - unless you have provided the object with a method called method_missing. The method_missing method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method.
Line 59: Line 56:
The above implementation of Numeric class allows calling euros as well as the euro method(since 1.euro is more appropriate). Here, since euro is not defined, method missing is passed the name of the method that does not exist: "euro" which can then be used to call the 'euros' method through self.send. Furthermore, if this is not defined, then the control is transfered to the parent class.  
The above implementation of Numeric class allows calling euros as well as the euro method(since 1.euro is more appropriate). Here, since euro is not defined, method missing is passed the name of the method that does not exist: "euro" which can then be used to call the 'euros' method through self.send. Furthermore, if this is not defined, then the control is transfered to the parent class.  


==3. Summary==
==Summary==
----
----
All the classes in Ruby are open which allow you to define custom methods.
All the classes in Ruby are open which allow you to define custom methods.
Line 65: Line 62:
These features saves a lot of effort in Ruby compared to other Object Oriented languages like JAVA, C++
These features saves a lot of effort in Ruby compared to other Object Oriented languages like JAVA, C++
==4. References==
==References==
----
----
http://rubylearning.com/blog/2010/11/23/dont-know-metaprogramming-in-ruby/
http://rubylearning.com/blog/2010/11/23/dont-know-metaprogramming-in-ruby/
Line 71: Line 68:
http://rubylearning.com/satishtalim/ruby_method_missing.html
http://rubylearning.com/satishtalim/ruby_method_missing.html


==5. Further Reading==
==Further Reading==
----
----
http://ruby-doc.org/docs/ProgrammingRuby/
http://ruby-doc.org/docs/ProgrammingRuby/

Revision as of 00:59, 4 October 2012

Introduction


One of the most impressive aspects of Ruby is its metaprogramming capabilities. As a dynamic language, Ruby gives you the freedom to define methods and even classes during runtime. Metaprogramming with Ruby, one can do in a few minutes what other languages may take hours to do. By cleverly planning your code and applying the techniques mentioned here, you’ll be able to write code that is DRYer, lighter, more intuitive and more scalable.

Metaprogramming in Ruby


Open Classes


In Ruby, classes are never closed: you can always add methods to an existing class. This applies to the classes you write as well as the standard, built-in classes. All you have to do is open up a class definition for an existing class, and the new contents you specify will be added to whatever's there.

Comparing this to other object oriented languages like JAVA, we cannot modify the existing standard classes and add custom methods. We will have to create a new class of our own which inherits from the standard class and add new functionalities to it. Then, we need to use this user-defined class in place of the standard class.

Implementing custom methods in Open Classes


As an example of Open Classes explained above, consider the functionality of converting Euros to Dollars.

class Numeric def euros ; self * 0.019; end # custom method defined here. end

class Bank .. account.deposit(20.euros) # call to custom method. .. end

Here, we define our own method 'euros' by extending the Numeric class. Euro-to-Dollar conversion can then be done by invoking the euros method as shown above.

Method Missing


When you send a message to an object, the object executes the first method it finds on its method lookup path with the same name as the message. If it fails to find any such method, it raises a NoMethodError exception - unless you have provided the object with a method called method_missing. The method_missing method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method.

If we extend the previous example to define method_missing for the Numeric class,//1.euro we can do it as shown:

class Numeric def method_missing(method_id) if method_id.to_s == "euro" self.send('euros') else super end end

def euros .. end end

The above implementation of Numeric class allows calling euros as well as the euro method(since 1.euro is more appropriate). Here, since euro is not defined, method missing is passed the name of the method that does not exist: "euro" which can then be used to call the 'euros' method through self.send. Furthermore, if this is not defined, then the control is transfered to the parent class.

Summary


All the classes in Ruby are open which allow you to define custom methods. To make code more readable, it is okay to reopen the standard classes to define custom methods. These features saves a lot of effort in Ruby compared to other Object Oriented languages like JAVA, C++

References


http://rubylearning.com/blog/2010/11/23/dont-know-metaprogramming-in-ruby/ http://rubylearning.com/satishtalim/ruby_open_classes.html http://rubylearning.com/satishtalim/ruby_method_missing.html

Further Reading


http://ruby-doc.org/docs/ProgrammingRuby/ http://en.wikipedia.org/wiki/Ruby_(programming_language) http://rubymonk.com/ http://www.ruby-lang.org/en/documentation/quickstart http://www.ruby-doc.org/downloads/