does Ruby has method properties

T

Thilina Buddhika

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
 
R

Ryan Davis

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.

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

Trans

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

Florian Gilcher

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

Xavier Noria

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

Phrogz

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
 
D

Daniel DeLorme

Trans said:
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! :)
 
F

Florian Frank

Peter said:

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

Trans

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

T.
 
G

gga

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?
 
J

Jari Williamsson

Trans said:
Well, I suppose there's always

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

It's a String now! ;)

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 Williamsson
 
T

Trans

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?

You mean to unify UnboundMethod and Method. I suppose UnboundMethod
could keep a table of the classes it was in.
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top