From: Howard Roberts Date: 2008-07-09T18:07:05+09:00 Subject: Re: type specific operator Amitraj singh Chouhan wrote: > These checkings will be done at run time. My question is if we have an > Idea about the type of x and y initially(at the time of writing the > program) can we eliminate these type checks. Why would you want to? Are you trying to solve a specific problem, or understand conceptually? Are you assuming some sort of efficiency gain by doing as you would like? > I mean is there any > operator or method call that will directly treat x and y as fixnum. i.e. > something like > int+(x,y) //specific function for adding two > integer > string+(x,y) //specific function for appending > two strings > or any thing like that which will directly treat x and y as variables > known types Objects such as Fixnum and String have a + method that they respond to: >> x=1 => 1 >> x.respond_to?("+") => true or not: >> h= {:a =>1} => {:a=>1} >> h2 = {:b => "foo"} => {:b=>"foo"} >> h.respond_to?("+") => false >> new_h = h+h2 NoMethodError: undefined method `+' for {:a=>1}:Hash from (irb):3 This is the idea behind DuckTyping. It doesn't matter the type of the object, so long as it responds to a specific message, all is well. As you see above, you can investigate for yourself ahead of time if the receiver responds to a particular message with respond_to?, but that doesn't really accomplish what you are looking for, specifically, in terms of the Ruby interpreter doing it's job at runtime. Howard -- Posted via http://www.ruby-forum.com/.