From: Trans Date: 2007-11-12T22:00:45+09:00 Subject: Re: does Ruby has method properties On Nov 12, 3:35 am, Thilina Buddhika wrote: > In java script it is possible to something like this, > method.outputType = "string"; > > we are setting the return type of the method. Is this possible in Ruby? > > i searched in the google and found this. > > http://bias2build.com/thesis/ruby_v_js_MP.html > > it says that this is not possible in Ruby. > > i wanna confirm this. > > help me please. It is possible in a round about way. In Ruby, methods are not first class objects. (Though to go with Ruby's 100% OOP mantra you would think they would be, but in any case...) You can make them behave as such for the most part if you want. You have to create a method cache, b/c #method itself will always return a new object rather than the same one. Facets does this with #method!. require 'facets/1stclassmethod' class X def initialize meth = method!('sayit') def meth.outputType "String" end end def sayit(msg) puts msg.to_s end def tellme method!('sayit').outputType end end puts X.new.tellme #=> String Besides #method! there is also #instance_method!. You have to realize the limitations of this though. In Ruby instance methods are not the same as object methods (shown above). So: X.instance_method!('sayit') != X.new.method!('sayit') T.