Does Ruby has method properties

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.

thanks!

regards,
buddhika

On Nov 12, 2007, at 00:35 , Thilina B. wrote:

it says that this is not possible in Ruby.

i wanna confirm this.

Confirmed. There is virtually no metadata about a method available to
the public. Arity is about the only one I can think of.

On Nov 12, 3:35 am, Thilina B. [email protected] wrote:

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.

On Mon, 12 Nov 2007 08:00:45 -0500, Trans wrote:

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.

Trans, you know I love your work, babe, but you’ve given a “Microsoft
tech
support talking to the guy in the helicopter” answer (or whatever your
favorite version of that joke is).

Yes, you’ve just created a method on a method, but the fact that it
happens
to return the word “String” doesn’t tell the guy anything prescriptive
about the return type. You could just as easily return “purple”, but it
doesn’t mean the result will be purple.

Actually, this cannot work in Ruby. In Java, the return type of a
method is defined, in ruby, a method has no return type.

Consider this:

def string_or_double(type)
if type == :string
return “String”
else
return 0.01
end
end

So this method can have the return type String or Double. Ruby doesn’t
care, so it doesn’t have and can’t even find out which
type is returned without executing the method.
Java, on the other hand, does care - it would only accept such a
construct if I defined the method to return a generic Object.

On Nov 12, 12:35 am, Thilina B. [email protected] 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?

No, you can’t set the return type of a method, but you can apply
metadata to describe a method. For example:
http://phrogz.net/RubyLibs/rdoc/classes/DescribeMethods.html

On Nov 12, 2007, at 3:25 PM, Jay L. wrote:

favorite version of that joke is).
Hey how’s that one :slight_smile:

On Nov 12, 5:00 am, Trans [email protected] wrote:
[…]

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…)
[…]

I used to think and wish for this too, and complain about it often.
Then, while implementing my own OOP version of Lua, I had an epiphany:

How can a method be a first-class object and have #super work?

For #super to work, you need a method to know what class it is
associated with, so that it can search the ancestor chain. But as soon
as you associate it with a class, it’s no longer a first class object.

(If you try to use the class of the receiver, it works for one level
up, but beyond that it fails.)

For more details, see my original post on this topic. [1] Please poke
holes in it. I’d love to see how Ruby could treat all methods as first-
class objects interchangeable with blocks/procs/lambdas, unifying the
madness (which, of course, would need some way to control the
different arity handling), but I’m not seeing it as possible right
now.

[1] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/179372
Feel free to poke holes in my argument

Trans wrote:

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’)

Why not? Come on Trans, you can make this work! :slight_smile:

Xavier N. wrote:

tech
support talking to the guy in the helicopter" answer (or whatever your
favorite version of that joke is).

Hey how’s that one :slight_smile:

http://www.google.com/search?q=microsoft+support+helicopter+joke

1st link:

http://www.bastichlabz.org/~tigger/TechHumor/mshelicopter.txt

:slight_smile:

Cheers,
Peter


http://www.rubyrailways.com
http://scrubyt.org

On Nov 12, 9:25 am, Jay L. [email protected] wrote:

Trans, you know I love your work, babe, but you’ve given a “Microsoft tech
support talking to the guy in the helicopter” answer (or whatever your
favorite version of that joke is).

Yes, you’ve just created a method on a method, but the fact that it happens
to return the word “String” doesn’t tell the guy anything prescriptive
about the return type. You could just as easily return “purple”, but it
doesn’t mean the result will be purple.

My bad! I didn’t realize Thilina was trying to land a helicopter :wink: I
was focusing on the metadata propellers and not the return landing
pad.

Well, I suppose there’s always

m = method(:foo)
define_method(:foo) do |str|
String(m.call(str))
end

It’s a String now! :wink:

T.

On Nov 12, 12:07 pm, Phrogz [email protected] wrote:

I used to think and wish for this too, and complain about it often.
Then, while implementing my own OOP version of Lua, I had an epiphany:

So is this Lua fork available somewhere?

Peter S. wrote:

Hey how’s that one :slight_smile:

microsoft support helicopter joke - Google Search

1st link:

http://www.bastichlabz.org/~tigger/TechHumor/mshelicopter.txt

I only knew this variant:

A manager and an engineer

A man is flying in a hot air balloon and realizes he is lost. He reduces
height and spots a man down below. He lowers the balloon further and
shouts: “Excuse me, can you help me? I promised my friend I would meet
him half an hour ago, but I don’t know where I am.”

The man below says: “Yes. You are in a hot air balloon, hovering
approximately 30 feet above this field. You are between 40 and 42
degrees N. latitude, and between 58 and 60 degrees W. longitude.”

“You must be an engineer” says the balloonist.

“I am” replies the man. “How did you know?”

“Well” says the balloonist, “everything you have told me is technically
correct, but I have no idea what to make of your information, and the
fact is I am still lost.”

The man below says “You must be a manager.”

“I am” replies the balloonist, “but how did you know?”

“Well”, says the man, “you don’t know where you are, or where you are
going. You have made a promise which you have no idea how to keep, and
you expect me to solve your problem. The fact is you are in the exact
same position you were in before we met, but now it is somehow my
fault.”

—8<------8<------8<------8<------8<------8<------8<------8<—

For some readers on this mailing list this might be a familiar
situation…

Trans wrote:

Well, I suppose there’s always

m = method(:foo)
define_method(:foo) do |str|
String(m.call(str))
end

It’s a String now! :wink:

IMO, it looks more Ruby to use the built in converter methods, even if
the effect are the same as your example above.

m.call(str).to_s

Best regards,

Jari W.

Thilina B. wrote:

In java script it is possible to something like this,
method.outputType = “string”;

we are setting the return type of the method.

Really? Care to provide an example?

Is this possible in Ruby?

i searched in the google and found this.

Ruby/Javascript Metaprogramming comparison

it says that this is not possible in Ruby.

It’s not possible in javascript either.

On Nov 12, 10:10 am, Phrogz [email protected] wrote:

How can a method be a first-class object and have #super work?
You mean to unify UnboundMethod and Method. I suppose UnboundMethod
could keep a table of the classes it was in.

madness (which, of course, would need some way to control the
different arity handling), but I’m not seeing it as possible right
now.

[1]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/179372
Feel free to poke holes in my argument

I’m not so sure about the #initialize thing. I think maybe your
troubles came from lua and how you went about it there. Ruby creates
an object basically like this:

class Class
def new(*a, &b)
o = allocate
o.initialize(*a, &b)
o
end
end

In fact you can use that as a basis for some fun meta-coding.

class Class
alias_method :postinitialize_new, :new
def new(*args, &blk)
o = allocate
a = ancestors
until a.empty?
m = a.pop
if m.method_defined?(‘preinitialize’) or
m.private_method_defined?(‘preinitialize’)
im = instance_method(‘preinitialize’)
im.arity == 0 ? im.bind(o).call : im.bind(o).call(*args,
&blk)
end
end
o.send(:initialize, *args, &blk) if
o.object_class.private_method_defined?(:initialize)
o
end
end

So i don’t see why it can’t be:

class Class
def new(*a, &b)
o = allocate
o.new(*a, &b)
o
end
end

also, from your post, I wish ruby did this always!:

“(For those not familiar with Lua, the colon used when defining a
function means “Hey, please create an implicit first parameter named
‘self’, because I’m too lazy to type it each time.” This is why I can
pass the ‘self’ from the subclasses to the parent method, and have the
initializer operate on it instead.)”

T.