Forum: Ruby object::foo syntax -- get a Method object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-06-02 06:12
(Received via mailing list)
Hi,

At the moment, "obj::foo" means the same as "obj.foo"; that is,
"obj.send(:foo)".

I propose that it be changed to mean "obj.method(:foo)" instead.

Although the obj.foo syntax is generally preferred, the obj::foo syntax
is commonly used for class methods in the standard library, so I assume
such a change is too incompatible for use in the 1.8 branch.

This would provide simpler access to method objects in case you want to
use them functionally, pass them around as blocks, etc. It would mean
that (as in Python, C++ and other languages) it's as easy to refer to a
method object as to call the method, whereas currently that requires a
wordy method call.

We don't need two such similar ways to call a method - the dot is fine.

What do you think?

Cheers,
Dave
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-02 12:27
(Received via mailing list)
Dave Burt wrote:
> At the moment, "obj::foo" means the same as "obj.foo"; that is,
> "obj.send(:foo)".
>
> I propose that it be changed to mean "obj.method(:foo)" instead.

I'm just not sure that method objects are accessed frequently enough to
justify such a syntax. Furthermore, if we *were* to introduce a
shorthand syntax for accessing method objects, why not use a syntax that
isn't currently in use?

   obj->foo
   obj~foo

etc.


Daniel
F2d901ea9430646c6dd35a629bb3f119?d=identicon&s=25 Simen Edvardsen (Guest)
on 2006-06-02 12:34
(Received via mailing list)
On 6/2/06, Dave Burt <dave@burt.id.au> wrote:
> Hi,
>
> At the moment, "obj::foo" means the same as "obj.foo"; that is,
> "obj.send(:foo)".
>
They're not the same:

$ irb
irb(main):001:0> class Test
irb(main):002:1>   class InnerTest
irb(main):003:2>   end
irb(main):004:1> end
=> nil
irb(main):005:0> Test.InnerTest
NoMethodError: undefined method `InnerTest' for Test:Class
        from (irb):5
        from :0
irb(main):006:0> Test::InnerTest
=> Test::InnerTest
irb(main):007:0>

> wordy method call.
>
method (six characters) is not wordy at all. Method objects are
convenient, but I don't think that justifies special syntax.

> We don't need two such similar ways to call a method - the dot is fine.
>
> What do you think?
>
> Cheers,
> Dave
>
>
I say leave it as it is.
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-06-02 12:55
(Received via mailing list)
On Fri, 2006-06-02 at 19:25 +0900, Daniel Schierbeck wrote:
>
>    obj->foo
>    obj~foo

I've been watching the related thread on the newsgroup, and I agree with
Daniel, in that I've not used #method enough to make it worthwhile
having a special syntax for it, and even if we did I think :: is
confusing enough already.

(Aside: Current ruby 1.9 has an experimental lambda syntax involving the
-> operator).
45196398e9685000d195ec626d477f0e?d=identicon&s=25 unknown (Guest)
on 2006-06-02 20:09
(Received via mailing list)
This is taken from facet/kernel/method.rb:

#--
# NOTES:
#
#   First Class Methods
#
#   I think the best solution would be using the notation
#   <tt>::ameth</tt>. This would require some minor changes
#   to Ruby, but with few backward incompatabilites if
#   parantheticals revert back to the actual method invocation.
#   Although this later stipulation means capitalized methods
#   would not be accessible in this way b/c they would intefere with
#   constant lookup. It's a trade off.
#
#                   Current           Proposed           Alternate
#                   ----------------- ------------------
-------------------
#     Foo.Bar()     method call       method call        method call
#     Foo.Bar       method call       method call        method call
#     Foo.bar()     method call       method call        method call
#     Foo.bar       method call       method call        method call
#     Foo::Bar()    method call       method call        1st class
method
#     Foo::Bar      constant lookup   constant lookup    constant
lookup
#     Foo::bar()    method call       method call        1st class
method
#     Foo::bar      method call       1st class method   1st class
method
#
#   Then again this dosen't address bound versus unbound!!!
#
#   Which do you prefer?
#++

class Object

  # Easy access to method as objects, and they retain state!
  #
  #   def hello
  #     puts "Hello World!"
  #   end
  #   p method!(:hello)   #=> <Method: #hello>
  #
  def method!(s)
    ( @__methods__ ||= {} )[s] ||= method(s)
  end

end
This topic is locked and can not be replied to.