CSC/ECE 517 Fall 2012/ch1 1w31 sa: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
== IDEs for Ruby ==
'''method_missing in Ruby:''' 


=== Overview ===


----
=='''Introduction<ref>http://rubylearning.com/satishtalim/ruby_method_missing.html</ref>'''==
A method of a class is called by creating an object of the class and passing the method name to the object as a message. The object then looks up into its [http://www.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html method lookup path] and tries to match the called method with the defined methods in the class. On success, the method is executed along with the parameters passed and the result is returned.


=== List of Popular IDEs for Ruby ===
If the object does not find a match in its method lookup, in normal circumstances the [http://www.ruby-doc.org/core-1.9.3/NoMethodError.html NoMethodError Exception] is raised .


----
“method_missing” is defined as a method in the class and is invoked if a called method is not found.
==== RadRails ====


----
=='''Format for defining method_missing'''==


===== History =====
=> def method_missing(m,*args,&block)
RadRails was started as an open source project by Kyle Shank, Marc Baumbach, and Matt Kent in September 2005 during their time as developers at IBM at Raleigh, NC. Along with Andy Gianfagna and Ryan Lowe, the project continued through 2006. Based on their idea for a startup, Kyle and Matt working on Persai<insert external link> with RadRails pushed as a side project. In the best interests of the RadRails community, Kyle handed over the project to Aptana who later on March 9, 2007 took over it and renamed it to Aptana RadRails.


===== Overview =====
(i) '''m->''' accepts the symbol/name of the undefined method (ii) '''*args->''' accepts the array of arguments passed in the method call  (iii) '''&block->'''accepts a block passed to the method
RadRails[http://en.wikipedia.org/wiki/RadRails] is a complete open source, web development tool supported by Aptana[http://www.aptana.com/]. RadRails is essentially a Rapid Application Development IDE for the Ruby on Rails[http://en.wikipedia.org/wiki/Ruby_on_Rails] framework. It comes bundled with an Integrated Debugger (for a more visual and intuitive debugging experience) and a Built-in Terminal (to run utilities like gem, rake etc.). The IDE can be customized to include custom scripts and commands. Some of the other salient features of RadRails include:


* HTML, CSS, and JavaScript Unification
=='''Method Lookup Path'''==
: Apart from Ruby and Rails, it supports development with HTML, CSS, Javascript, PHP and Python. It also provides support for latest versions of popular browsers along with HTML5 and CSS3.
The object of a class receives a method name to be executed, the following steps are carried out for matching and executing the method:
* Deployment Wizard
: Provides support for multiple protocols like FTP, SFTP, FTPS and Capistrano. Enables support for continuous and multiple synchronized setups and integration and deployment to hosting services like Heroku[http://www.heroku.com/].
* Git Integration
: Supports most Github based functions like git source code control, collaborating with team mates to use merge, pull and push to repositories set up at Github or other servers. Git-based deployments are also supported.


===== Architecture =====
*First, the object looks in its own [http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html instance methods].
===== Advantages =====
*Second, it looks in the list of instance methods that all objects of that class share.
===== Disadvantages =====
*Third, in each of the included [http://www.tutorialspoint.com/ruby/ruby_modules.html modules] of that class, in reverse order of inclusion.
*Fourth, it looks in that class’s superclass.
*Fifth, in the superclass’s included modules, all the way up until it reaches the class Object.
*Sixth, if it still can’t find a method, the very last place it looks is in the [http://linux.die.net/lkmpg/x40.html Kernel module], included in the class Object.
*Finally, it calls method_missing (if defined in the class), else throws up the NOMethodError exception.


This entire tracing that the object does is the method lookup path.


==== Netbeans ====
=='''Examples'''==
----
===Calling defined and undefined methods===


===== History =====
class A // creating a class 'A'
===== Overview =====
def say // defining a method 'say'
===== Architecture =====
puts " say Hi " // body of method say
===== Advantages =====
end
===== Disadvantages =====
end


==== Textmate ====
Creating the object of the class
----
  a=A.new       // object of the class
  => #<A:0x2a082e0>    //object id


===== History =====
Calling the defined method
===== Overview =====
===== Architecture =====
===== Advantages =====
===== Disadvantages =====


a.say                  // defined method
=> say Hi       // returned result


==== VIM ====
Calling the undefined method
----
a.sayhi                // undefined method sayhi
NoMethodError: undefined method `sayhi' for #<A:0x2a082e0>  // the NoMethodError is raised


===== History =====
===method_missing implementation<ref>http://www.thirdbit.net/articles/2007/08/01/10-things-you-should-know-about-method_missing/</ref>===
===== Overview =====
===== Architecture =====
===== Advantages =====
===== Disadvantages =====


class A
def say
puts " say hi "
end
def method_missing(m,*args,&block) // defining method_missing
puts " This method does not exist" // body of method_missing
end
end


==== RubyMine ====
Calling a method that is not defined
----
a=A.new
a.sayhi                                // calling the undefined method sayhi with no arguments
=> This method does not exist // this result returned when method_missing is executed


===== History =====
When the object 'a' traces its method lookup path for a matching method as 'sayhi', upon failure it resorts to method_missing and the body of method_missing is executed. 'method_missing' is the last resort.
===== Overview =====
===== Architecture =====
===== Advantages =====
===== Disadvantages =====




==== EMACs ====
Sometimes when a class has many methods that do generally the same kinds of things, and the programmer is not sure in advance which methods the user will call since there are so many of them, and they are all so similar, implementing all of them by hand seems futile. In these situations method_missing makes a new method that was previously not defined and adds it to the class. The below 'Generic Handler' example implements this.
----


===== History =====
===passing parameters to an undefined method call===
===== Overview =====
===== Architecture =====
===== Advantages =====
===== Disadvantages =====


class A
def add(a,b)
a+b
end
def method_missing(name,*args,&block)    // the method_missing is defined and the *args parameter accepts all the parameters passed during                                
                                            the method call 
puts “You have typed the method name wrong and these were the parameters passed ; #{args[0]}, #{args[1]}”
end                       
end


==== ETextEditor ====
The passed parameters are stored in the array 'args' and can be accessed like a normal array               
----
                                                 
Calling the defined method


===== History =====
a.add(1,2)         // calling the defined method add and passing the parameters (1,2)
===== Overview =====
=> 3                          // result
===== Architecture =====
===== Advantages =====
===== Disadvantages =====


Calling the undefined method
a.adds(4,2) // calling the undefined method adds and passing the parameter (4,2)
=> You have typed the method name wrong and these were the parameters passed; 4, 2


==== Komodo ====
The user made a genuine mistake by typing 'adds', but this method is not defined. When the 'adds' method with parameters is called, the object 'a' tries to match the method in the method lookup path. Upon failure it invokes method_missing, the args are passed, stored in the array 'args' and the body of method_missing is executed.
----


===== History =====
===converting numbers from roman representation to integer representation<ref>http://www.rubyquiz.com/quiz22.html</ref>===
===== Overview =====
class Roman
===== Architecture =====
@@Roman_to_Numeric = {'i' => 1, 'v' => 5, 'x' => 10, 'l' => 50, 'c' => 100, 'd' => 500, 'm' => 1000} 
===== Advantages =====
def method_missing(method_var,*args,&block)
===== Disadvantages =====
numeric_value = 0
roman_string = method_var.to_s.downcase
for i in 0...roman_string.length-1
if (cond) == nil
else
puts "Roman string is invalid"
return
end
end
while roman_string != ""
if roman_string[roman_string.length - 1] == 'x' && roman_string[roman_string.length - 2] == 'i'
numeric_value += 9
roman_string.chop!
roman_string.chop!
elsif
roman_string[roman_string.length - 1] == 'v' && roman_string[roman_string.length - 2] == 'i'
numeric_value += 4
roman_string.chop!
roman_string.chop!
else
numeric_value += @@Roman_to_Numeric[roman_string[(roman_string.length)-1]]
roman_string.chop!
end
end
puts "Numeric Value for #{method_var} is: #{numeric_value}"
end
end
'''cond''' = '''['''@@Roman_to_Numeric[roman_string[i]]-@@Roman_to_Numeric[roman_string[i+1]] == 0 || @@Roman_to_Numeric[roman_string[i]]-@@Roman _ to_                              Numeric [ roman _ string [ i + 1 ] ]  == - 9 || @@Roman_to_Numeric[roman_string[i]]-@@Roman_to_Numeric[roman_string[i+1]] == -4    ||    @@Roman_to_Numeric[roman_string[i]]-@@Roman_to_Numeric[roman_string[i+1]] >= 4) && (/.v.x/ =~ roman_string''']'''




==== RDT (Ruby Plugin for Eclipse) ====
Calling the undefined methods
----


===== History =====
r= Roman.new
===== Overview =====
r.vii
===== Architecture =====
r.iClx
===== Advantages =====
r.xxix
===== Disadvantages =====
r.xxxi
r.xxiv
r.xxvi
r.vx


The Output


==== Ruby on Steel (VST Plugin for Ruby)====
Numeric Value for vii is: 7
----
Roman string is invalid
Numeric Value for xxix is: 29
Numeric Value for xxxi is: 31
Numeric Value for xxiv is: 24
Numeric Value for xxvi is: 26
Roman string is invalid


===== History =====
===method_missing to log method calls<ref>http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2007/wiki1b_2_22</ref>===
===== Overview =====
Another application that makes use of method_missing could be a simple logger used for debugging purposes. Many times, it may be required to log the trace of called methods and provide information such as: called method-name, arguments, return type. It can be tedious to repeat this part of code in every method. A simple solution to this problem can be obtained using method_missing as:
===== Architecture =====
===== Advantages =====
===== Disadvantages =====


=== Comparison Table of Popular IDEs for Ruby ===
  class SimpleCallLogger
----
  def initialize(o)
  @obj = o
  end
  def method_missing(methodname, *args)
  puts "called: #{methodname}(#{args})"
  a = @obj.send(methodname, *args)
  puts "\t-> returned: #{a}"
  return a
  end
  end


=== See Also ===
This program makes use of method_missing in a way that it wraps around called method to output the logging information on entry and on exit, it logs the return type. Further, method_missing intercepts the method call and forward it to internal object with ‘send’ method of ruby. Hence, this use of method_missing acts as wrapper.
----


=== References ===
===Generic Handler===
----
class NoBar
def method_missing(methodname, *args)
define_method(:bar) if "bar" == methodname.to_s
define_method(:nobar) if "nobar" == methodname.to_s
end
end


=== Additional References ===
This is an example of using method_missing as a generic handler to handle when a calling method is not exist. You can use missing_method to dynamically create a method at a runtime.
----
 
=='''Advantages of method_missing'''==
 
*In addition to specifying the error messages for the undefined methods, method_missing provides a more dynamic behavior in the programming environment.
*If we are unfamiliar with the usage of the objects we created, then using method_missing is a good technique.
*Handles problems at runtime.
*Define's a generic method_missing and handle's any undefined method, a big advantage over Java. In Java, when you call an undefined method, the program will not compile.
*method_missing falls under the general technique of [http://en.wikipedia.org/wiki/Metaprogramming meta-programming]. Employ meta-programming in missing_function to write an another function to handle the call.
 
=='''Disadvantages of method_missing'''==
*Slower than conventional method lookup. Simple tests indicate that method dispatch with method_missing is at least two to three times as expensive in time as conventional dispatch.
 
*Since the methods being called never actually exist—they are just intercepted at the last step of the method lookup process—they cannot be documented or introspected as conventional methods can.
 
*[http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html Dynamic methods] must go through the method_missing method, the body of that method can become quite large if there are many different aspects of the code that needs to add methods dynamically.
 
*method_missing restricts compatibility with future versions of an [http://en.wikipedia.org/wiki/Application_programming_interface API]. Introducing new methods in a future API version can break users' expectations.
 
=='''Key Points'''==
*Ruby knows method_missing( ) exisits, because it's a private instance method of 'BasicObject' that every object inherits. The BasicObject#method_missing( ) responds by raising the NoMethodError. Overriding this method_missing( ) allows you to call methods that don't really exist.
 
*If method_missing is only looking for certain method names, don't forget to call the [http://en.wikibooks.org/wiki/Java_Programming/Keywords/super super] keyword if you haven't found what you're looking for, so that the other superclass' method_missing can handle it.
 
*[http://www.prateekdayal.net/2007/10/16/rubys-responds_to-for-checking-if-a-method-exists/ obj.respond_to?] function returns 'true' if the obj responds to the given method. So if you want to know whether your class will respond to a function you can use respond_to? to know the answer. But if method_missing() is used, the output may not be what you expect.
 
*Below class A defines only method method_missing() and no other method. When a new object of class A tries to access a method say 'foo', the respond_to? will return the value 'false'. When you actually try to implement it using “a.foo”, the code will get executed, courtesy of method_missing(). Even though the respond_to? says that you cannot access 'foo' method using object of class A, if you have method_missing() defined, you can access the method.
 
class A
def method_missing(method_id)
puts "In method_missing"
end
end
 
a = A.new
puts a.respond_to?(:foo)
a.foo
 
Output
false
In method_missing
 
*If within method_missing() we define an undefined method.
 
class A
@@i = 0
def method_missing(method_id)
puts "In Method Missing #{@@i}"
@@i += 1
self.fun
end
end
 
a = A.new
puts a.respond_to?(:foo) // just checking whether the receiver 'a' responds to the method 'foo'
 
a.foo
 
Output
 
The expected result would be a 'stack level too deep' error.
 
When the 'foo' method is called, after no method match the method_missing is run and this block has a method “self.fun” that is undefined. Here when the program execution encounters 'self.fun' it once again calls method_missing. This goes on in an endless loop till the stack memory is full.
 
=='''References'''==
<references/>
 
=='''Further Suggested Reading'''==
*http://www.rubyinside.com/
*http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html
*http://liquiddevelopment.blogspot.com/2006/04/twisting-and-shaping-dsls-using-ruby.html
*http://blog.rubybestpractices.com/posts/gregory/031-issue-2-method-lookup.html
*http://phrogz.net/RubyLibs/RubyMethodLookupFlow.pdf

Revision as of 01:54, 13 September 2012

method_missing in Ruby:


Introduction<ref>http://rubylearning.com/satishtalim/ruby_method_missing.html</ref>

A method of a class is called by creating an object of the class and passing the method name to the object as a message. The object then looks up into its method lookup path and tries to match the called method with the defined methods in the class. On success, the method is executed along with the parameters passed and the result is returned.

If the object does not find a match in its method lookup, in normal circumstances the NoMethodError Exception is raised .

“method_missing” is defined as a method in the class and is invoked if a called method is not found.

Format for defining method_missing

=> def method_missing(m,*args,&block)

(i) m-> accepts the symbol/name of the undefined method (ii) *args-> accepts the array of arguments passed in the method call (iii) &block->accepts a block passed to the method

Method Lookup Path

The object of a class receives a method name to be executed, the following steps are carried out for matching and executing the method:

  • First, the object looks in its own instance methods.
  • Second, it looks in the list of instance methods that all objects of that class share.
  • Third, in each of the included modules of that class, in reverse order of inclusion.
  • Fourth, it looks in that class’s superclass.
  • Fifth, in the superclass’s included modules, all the way up until it reaches the class Object.
  • Sixth, if it still can’t find a method, the very last place it looks is in the Kernel module, included in the class Object.
  • Finally, it calls method_missing (if defined in the class), else throws up the NOMethodError exception.

This entire tracing that the object does is the method lookup path.

Examples

Calling defined and undefined methods

class A		// creating a class 'A'
def say		// defining a method 'say'
puts " say Hi " // body of method say
end
end

Creating the object of the class

 a=A.new	       // object of the class
 => #<A:0x2a082e0>     //object id

Calling the defined method

a.say                  // defined method
=> say Hi	       // returned result

Calling the undefined method

a.sayhi                // undefined method sayhi
NoMethodError: undefined method `sayhi' for #<A:0x2a082e0>   // the NoMethodError is raised

method_missing implementation<ref>http://www.thirdbit.net/articles/2007/08/01/10-things-you-should-know-about-method_missing/</ref>

class A
def say
puts " say hi "
end
def method_missing(m,*args,&block)	// defining method_missing
puts " This method does not exist"	// body of method_missing
end
end

Calling a method that is not defined

a=A.new
a.sayhi                                // calling the undefined method sayhi with no arguments
=> This method does not exist		// this result returned when method_missing is executed

When the object 'a' traces its method lookup path for a matching method as 'sayhi', upon failure it resorts to method_missing and the body of method_missing is executed. 'method_missing' is the last resort.


Sometimes when a class has many methods that do generally the same kinds of things, and the programmer is not sure in advance which methods the user will call since there are so many of them, and they are all so similar, implementing all of them by hand seems futile. In these situations method_missing makes a new method that was previously not defined and adds it to the class. The below 'Generic Handler' example implements this.

passing parameters to an undefined method call

class A
def add(a,b)
a+b
end
def method_missing(name,*args,&block)    // the method_missing is defined and the *args parameter accepts all the parameters passed during 								                                 
                                            the method call   
puts “You have typed the method name wrong and these were the parameters passed ; #{args[0]}, #{args[1]}”								
end                         			
end

The passed parameters are stored in the array 'args' and can be accessed like a normal array


Calling the defined method

a.add(1,2)		        // calling the defined method add and passing the parameters (1,2)
=> 3                           // result

Calling the undefined method

a.adds(4,2) 			// calling the undefined method adds and passing the parameter (4,2)
=> You have typed the method name wrong and these were the parameters passed; 4, 2

The user made a genuine mistake by typing 'adds', but this method is not defined. When the 'adds' method with parameters is called, the object 'a' tries to match the method in the method lookup path. Upon failure it invokes method_missing, the args are passed, stored in the array 'args' and the body of method_missing is executed.

converting numbers from roman representation to integer representation<ref>http://www.rubyquiz.com/quiz22.html</ref>

class Roman
@@Roman_to_Numeric = {'i' => 1, 'v' => 5, 'x' => 10, 'l' => 50, 'c' => 100, 'd' => 500, 'm' => 1000}   
def method_missing(method_var,*args,&block)
numeric_value = 0
roman_string = method_var.to_s.downcase
for i in 0...roman_string.length-1
if (cond) == nil
else
puts "Roman string is invalid"
return
end
end
while roman_string != ""
if roman_string[roman_string.length - 1] == 'x' && roman_string[roman_string.length - 2] == 'i'
numeric_value += 9
roman_string.chop!
roman_string.chop!
elsif
roman_string[roman_string.length - 1] == 'v' && roman_string[roman_string.length - 2] == 'i'
numeric_value += 4
roman_string.chop!
roman_string.chop!
else
numeric_value += @@Roman_to_Numeric[roman_string[(roman_string.length)-1]]
roman_string.chop!
end
end
puts "Numeric Value for #{method_var} is: #{numeric_value}"
end
end

cond = [@@Roman_to_Numeric[roman_string[i]]-@@Roman_to_Numeric[roman_string[i+1]] == 0 || @@Roman_to_Numeric[roman_string[i]]-@@Roman _ to_ Numeric [ roman _ string [ i + 1 ] ] == - 9 || @@Roman_to_Numeric[roman_string[i]]-@@Roman_to_Numeric[roman_string[i+1]] == -4 || @@Roman_to_Numeric[roman_string[i]]-@@Roman_to_Numeric[roman_string[i+1]] >= 4) && (/.v.x/ =~ roman_string]


Calling the undefined methods

r= Roman.new
r.vii
r.iClx
r.xxix
r.xxxi
r.xxiv
r.xxvi
r.vx

The Output

Numeric Value for vii is: 7
Roman string is invalid
Numeric Value for xxix is: 29
Numeric Value for xxxi is: 31
Numeric Value for xxiv is: 24
Numeric Value for xxvi is: 26
Roman string is invalid

method_missing to log method calls<ref>http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2007/wiki1b_2_22</ref>

Another application that makes use of method_missing could be a simple logger used for debugging purposes. Many times, it may be required to log the trace of called methods and provide information such as: called method-name, arguments, return type. It can be tedious to repeat this part of code in every method. A simple solution to this problem can be obtained using method_missing as:

 class SimpleCallLogger
 def initialize(o)
 @obj = o
 end
 def method_missing(methodname, *args)
 puts "called: #{methodname}(#{args})"
 a = @obj.send(methodname, *args)
 puts "\t-> returned: #{a}"
 return a
 end
 end

This program makes use of method_missing in a way that it wraps around called method to output the logging information on entry and on exit, it logs the return type. Further, method_missing intercepts the method call and forward it to internal object with ‘send’ method of ruby. Hence, this use of method_missing acts as wrapper.

Generic Handler

class NoBar
def method_missing(methodname, *args)
define_method(:bar) if "bar" == methodname.to_s
define_method(:nobar) if "nobar" == methodname.to_s
end
end

This is an example of using method_missing as a generic handler to handle when a calling method is not exist. You can use missing_method to dynamically create a method at a runtime.

Advantages of method_missing

  • In addition to specifying the error messages for the undefined methods, method_missing provides a more dynamic behavior in the programming environment.
  • If we are unfamiliar with the usage of the objects we created, then using method_missing is a good technique.
  • Handles problems at runtime.
  • Define's a generic method_missing and handle's any undefined method, a big advantage over Java. In Java, when you call an undefined method, the program will not compile.
  • method_missing falls under the general technique of meta-programming. Employ meta-programming in missing_function to write an another function to handle the call.

Disadvantages of method_missing

  • Slower than conventional method lookup. Simple tests indicate that method dispatch with method_missing is at least two to three times as expensive in time as conventional dispatch.
  • Since the methods being called never actually exist—they are just intercepted at the last step of the method lookup process—they cannot be documented or introspected as conventional methods can.
  • Dynamic methods must go through the method_missing method, the body of that method can become quite large if there are many different aspects of the code that needs to add methods dynamically.
  • method_missing restricts compatibility with future versions of an API. Introducing new methods in a future API version can break users' expectations.

Key Points

  • Ruby knows method_missing( ) exisits, because it's a private instance method of 'BasicObject' that every object inherits. The BasicObject#method_missing( ) responds by raising the NoMethodError. Overriding this method_missing( ) allows you to call methods that don't really exist.
  • If method_missing is only looking for certain method names, don't forget to call the super keyword if you haven't found what you're looking for, so that the other superclass' method_missing can handle it.
  • obj.respond_to? function returns 'true' if the obj responds to the given method. So if you want to know whether your class will respond to a function you can use respond_to? to know the answer. But if method_missing() is used, the output may not be what you expect.
  • Below class A defines only method method_missing() and no other method. When a new object of class A tries to access a method say 'foo', the respond_to? will return the value 'false'. When you actually try to implement it using “a.foo”, the code will get executed, courtesy of method_missing(). Even though the respond_to? says that you cannot access 'foo' method using object of class A, if you have method_missing() defined, you can access the method.
class A
def method_missing(method_id)
puts "In method_missing"
end
end
a = A.new
puts a.respond_to?(:foo)
a.foo

Output

false
In method_missing
  • If within method_missing() we define an undefined method.
class A
@@i = 0
def method_missing(method_id)
puts "In Method Missing #{@@i}"
@@i += 1
self.fun
end
end
a = A.new
puts a.respond_to?(:foo)	// just checking whether the receiver 'a' responds to the method 'foo'
a.foo 

Output

The expected result would be a 'stack level too deep' error. 

When the 'foo' method is called, after no method match the method_missing is run and this block has a method “self.fun” that is undefined. Here when the program execution encounters 'self.fun' it once again calls method_missing. This goes on in an endless loop till the stack memory is full.

References

<references/>

Further Suggested Reading