The importance of Ruby’s method missing concept
To be frankly, I don't have any previous experience with Ruby programming. I first looked at Ruby code(WSF/Ruby source), when I started to write WSF/Python and WSF/Python is still in its initial stage. When I started to implement WSDL mode for WSF/Python, to understand how it has done in WSF/Ruby, I looked at WSF/Ruby source. While I am reading through the code I found this ruby class called WSProxy, which handles the all the WSDL mode stuff. It has only two methods, 'initialize' method and 'method_missing' method. When I saw this code I looked at sample code that uses this WSProxy class. In that sample there is some code like following wich execute undefined method(because it's not on the actual implementation of WSProxy class) on instance of WSProxy. After a small discussion with the developer behind this code, I learn that actually the 'method_missing' method in WSProxy class is doing the magic. When we execute undefined method on Ruby's objects, all of these undefined calls are end up in this method. Inside this method you can get the name of that undefined methods and arguments given to it. Ruby has this nice feature called method_missing method for every object in Ruby. Method missing is where your method request will end up if the method you called cannot be found. Here is a simple example:class String def method_missing(method, *arg) puts "Called: #{method}" return nil end end puts "Hello".to_int()
Called: to_int nil
client = WSClient.new({"wsdl" => END_POINT}, LOG_FILE_NAME) proxy = client.get_proxy unless proxy.nil? ret = proxy.QueryPurchaseOrder({"orderInfo"=> {"productName"=> "Testing", "quantity" => 234, "date" => "2003-12-34", "orderNo" => 345}});
def method_missing(name, *args) if args.length == 1 then arg = args[0] if arg.kind_of? Hash then result = WSFC::wsf_wsdl_request_function(@env, @svc_client, @options, @wsdl, name.to_s, arg, @service_name, @port_name, @ruby_home) return result else puts "only Hash is supported" end else puts "dynamic function only support one argument" end return nil end
Related posts brought to you by Yet Another Related Posts Plugin.



Rubymethod lookup flow (August 9, 2008, 4:24 pm).
[...] a previous post I describe the advantages of Ruby method_missing feature. Here is good diagram which shows the flow of Ruby method [...]