does Ruby has method properties

Discussion in 'Ruby' started by Thilina Buddhika, Nov 12, 2007.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
    Thilina Buddhika, Nov 12, 2007
    #1
    1. Advertising

  2. Thilina Buddhika

    Ryan Davis Guest

    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_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.
    Ryan Davis, Nov 12, 2007
    #2
    1. Advertising

  3. Thilina Buddhika

    Trans Guest

    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.
    Trans, Nov 12, 2007
    #3
  4. 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_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.
    >
    >
    Florian Gilcher, Nov 12, 2007
    #4
  5. Thilina Buddhika

    Xavier Noria Guest

    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 :)
    Xavier Noria, Nov 12, 2007
    #5
  6. Thilina Buddhika

    Phrogz Guest

    On Nov 12, 5:00 am, Trans <> 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
    Phrogz, Nov 12, 2007
    #6
  7. Thilina Buddhika

    Phrogz Guest

    On Nov 12, 12: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?


    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
    Phrogz, Nov 12, 2007
    #7
  8. Thilina Buddhika

    Peter Szinek Guest

    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 helicopter joke

    1st link:

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

    :)

    Cheers,
    Peter
    ___
    http://www.rubyrailways.com
    http://scrubyt.org
    Peter Szinek, Nov 12, 2007
    #8
  9. 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! :)
    Daniel DeLorme, Nov 14, 2007
    #9
  10. Peter Szinek wrote:
    >> Hey how's that one :)

    >
    > http://www.google.com/search?q=microsoft support helicopter joke
    >
    > 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...

    --
    Florian Frank
    Florian Frank, Nov 14, 2007
    #10
  11. Thilina Buddhika

    Trans Guest

    On Nov 12, 9:25 am, Jay Levitt <> 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 ;) 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.
    Trans, Nov 14, 2007
    #11
  12. Thilina Buddhika

    gga Guest

    On Nov 12, 12:07 pm, Phrogz <> 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?
    gga, Nov 14, 2007
    #12
  13. Thilina Buddhika

    7stud -- Guest

    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.


    Really? Care to provide an example?


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



    It's not possible in javascript either.
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Nov 14, 2007
    #13
  14. 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! ;)


    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
    Jari Williamsson, Nov 14, 2007
    #14
  15. Thilina Buddhika

    Trans Guest

    On Nov 12, 10:10 am, Phrogz <> wrote:
    > On Nov 12, 5:00 am, Trans <> 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?


    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.
    Trans, Nov 14, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    0
    Views:
    910
    Nathan Sokalski
    Oct 17, 2005
  2. =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=

    CompositeControls: ViewState properties w/ Mapped properties probl

    =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,138
    Steven Cheng[MSFT]
    Jan 19, 2006
  3. Kent Lichty
    Replies:
    0
    Views:
    833
    Kent Lichty
    Apr 16, 2004
  4. Mars
    Replies:
    2
    Views:
    412
    Bengt Richter
    Jul 19, 2003
  5. Sonal
    Replies:
    3
    Views:
    667
Loading...

Share This Page