[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

does Ruby has method properties

Thilina Buddhika

11/12/2007 8:35:00 AM

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_...

it says that this is not possible in Ruby.

i wanna confirm this.

help me please.

thanks!

regards,
buddhika
--
Posted via http://www.ruby-....

15 Answers

Ryan Davis

11/12/2007 10:10:00 AM

0


On Nov 12, 2007, at 00:35 , 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_...
>
> 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.


Trans

11/12/2007 1:01:00 PM

0



On Nov 12, 3:35 am, Thilina Buddhika <thilin...@gmail.com> 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_...
>
> 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.


Florian Gilcher

11/12/2007 2:04:00 PM

0

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, 2007, at 11:09 AM, Ryan Davis wrote:

>
> On Nov 12, 2007, at 00:35 , 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_...
>>
>> 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.
>
>


Jay Levitt

11/12/2007 2:21:00 PM

0

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.

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://... | - Kristoffer

Xavier Noria

11/12/2007 2:59:00 PM

0

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

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

Hey how's that one :-)

Phrogz

11/12/2007 3:08:00 PM

0

On Nov 12, 5:00 am, Trans <transf...@gmail.com> 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-t...
Feel free to poke holes in my argument

Phrogz

11/12/2007 3:10:00 PM

0

On Nov 12, 12:35 am, Thilina Buddhika <thilin...@gmail.com> 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/DescribeMe...

Peter Szinek

11/12/2007 3:19:00 PM

0

Xavier Noria wrote:
> On Nov 12, 2007, at 3:25 PM, Jay Levitt wrote:
>
>> 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).
>
> Hey how's that one :-)

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

1st link:

http://www.bastichlabz.org/~tigger/TechHumor/msheli...

:-)

Cheers,
Peter
___
http://www.rubyra...
http://s...




Daniel DeLorme

11/14/2007 1:32:00 AM

0

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! :-)

Florian Frank

11/14/2007 2:16:00 AM

0

Peter Szinek wrote:
>> Hey how's that one :-)
>
> http://www.google.com/search?q=microsoft+support+helic...
>
> 1st link:
>
> http://www.bastichlabz.org/~tigger/TechHumor/msheli...

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...

--
Florian Frank