# Object::foo syntax -- get a Method object?

**URL:** <https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282>\
**Category:** Ruby\
**Created:** [June 2, 2006, 6:12am UTC](https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282 "2006-06-02T06:12:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [June 2, 2006, 6:12am UTC](https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282/1 "2006-06-02T06:12:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [June 2, 2006, 12:27pm UTC](https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282/2 "2006-06-02T12:27:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [June 2, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282/3 "2006-06-02T12:34:04Z")

</div>

On 6/2/06, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [June 2, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282/4 "2006-06-02T12:55:46Z")

</div>

On Fri, 2006-06-02 at 19:25 +0900, Daniel S. 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).

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [June 2, 2006, 8:09pm UTC](https://www.ruby-forum.com/t/object-foo-syntax-get-a-method-object/62282/5 "2006-06-02T20:09:43Z")

</div>

This is taken from facet/kernel/method.rb:

#–

# NOTES:

# 

# First Class Methods

# 

# I think the best solution would be using the notation

# ::ameth. 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
