square(11) is interpreted as "call the method 'square' on the current
"self" object, with the argument '11'", whereas square[11] is
interpreted as "call the method on the object 'square' with the
argument 11. Proc defines to be a synonym of call.
martin
···
On 8/24/06, Dave Rose <bitdoger2@yahoo.com> wrote:
in The Ruby Way...chap 1...a crude example of closure doesn't work as
this irb
session:
>> def power(exponent)
>> proc {|base| base**exponent}
>> end
=> nil
>> square = power(2)
=> #<Proc:0x02dc58f0@(irb):2>
>> cube = power(3)
=> #<Proc:0x02dc58f0@(irb):2>
>> p square
#<Proc:0x02dc58f0@(irb):2>
=> nil
>> square(11)
NoMethodError: undefined method `square' for main:Object
from (irb):7
Is there any way to have a proc object be callable identically to a method,
ie with the parentheses? Is there a reason to keep the notation distinct?
Thanks,
-rr-
···
On 8/24/06, Martin DeMello <martindemello@gmail.com> wrote:
On 8/24/06, Dave Rose <bitdoger2@yahoo.com> wrote:
> in The Ruby Way...chap 1...a crude example of closure doesn't work as
> this irb
> session:
> >> def power(exponent)
> >> proc {|base| base**exponent}
> >> end
> => nil
> >> square = power(2)
> => #<Proc:0x02dc58f0@(irb):2>
> >> cube = power(3)
> => #<Proc:0x02dc58f0@(irb):2>
> >> p square
> #<Proc:0x02dc58f0@(irb):2>
> => nil
> >> square(11)
> NoMethodError: undefined method `square' for main:Object
> from (irb):7
You want square[11] or square.call(11)
square(11) is interpreted as "call the method 'square' on the current
"self" object, with the argument '11'", whereas square[11] is
interpreted as "call the method on the object 'square' with the
argument 11. Proc defines to be a synonym of call.
Is there any way to have a proc object be callable identically to a method,
ie with the parentheses? Is there a reason to keep the notation distinct?
I think it's difficult. Ruby distinguishes between method names
and variable names by how they are used. In this case, you'd be
using the same name both ways.
On Fri, Aug 25, 2006 at 03:27:10AM +0900, ara.t.howard@noaa.gov wrote:
On Fri, 25 Aug 2006, rak rok wrote:
>Is there any way to have a proc object be callable identically to a method,
>ie with the parentheses? Is there a reason to keep the notation distinct?
>
>Thanks,
>-rr-
harp:~ > cat a.rb
def the_method() 'namespace one' end
I think it's difficult. Ruby distinguishes between method names
and variable names by how they are used. In this case, you'd be
using the same name both ways.
Perhaps this could be an rcr? I'm having trouble seeing why Ruby would
impose a syntactic difference between a proc and a method call. It would be
nice syntactic sugar IMO.
I think it's difficult. Ruby distinguishes between method names
and variable names by how they are used. In this case, you'd be
using the same name both ways.
Perhaps this could be an rcr? I'm having trouble seeing why Ruby would
impose a syntactic difference between a proc and a method call. It would be
nice syntactic sugar IMO.
I just tried to explain why. A proc is stored in a variable.
A variable can't have () after it, because it will look like
a method call.
It's *conceivable* this could be changed, but then people would
(accidentally or on purpose) try to "call" a variable that
didn't refer to a proc:
a = 5
a()
Then Ruby would have to be smart enough to call the method a
rather than trying to call the proc that isn't referred to
by a.
Furthermore, suppose you have both. Which takes precedence?
Read up on the difference between a lisp 1 and a lisp 2. (Which is not
intended to be condescending or dismissive; just that reams have been
written on the topic and I see no point in rehashing it imperfectly in
here.) Essentially it boils down to whether functions and variables
share a namespace or not.
martin
···
On 8/25/06, rak rok <rakrok@gmail.com> wrote:
> a = 5
> a()
This would fail just like a.call() would fail.
def say
> puts "hello"
> end
>
> say = proc { puts "goodbye" }
>
> say() # hello or goodbye?
I would say this would be similar to redefining a method, where the latest
definition takes precedence.
There are languages (Scheme is one if I remember correctly) where the two are semantically equivalent and thus () is used as a call operator for both types of definitions. All functions / methods are anonymous unless bound to a name. In such a language this would indeed be a redefinition of the method as def is simply syntactic sugar. This is approach is clean and simple in many respects, but is different than the approach Ruby takes. Unless Ruby were to change its semantics (not likely) I think David is right that this approach won't work in Ruby.
Matthew
···
On Fri, 25 Aug 2006, rak rok wrote:
a = 5
a()
This would fail just like a.call() would fail.
def say
puts "hello"
end
say = proc { puts "goodbye" }
say() # hello or goodbye?
I would say this would be similar to redefining a method, where the latest
definition takes precedence.
That's never going to work. It would quickly become a nightmare of
scoping and naming issues.
Methods and Procs just aren't at the same level of indirection from
their written identifiers. It's better to keep that difference
visible.
. . . except that in Lisps the () is list syntax, and the reason
closures and methods share that syntax is that everything is a list.
That's my understanding, anyway. Then again, I'm no Lisp expert.
On the other hand, Perl might be a good example of unified syntax for
methods and closures. In both cases, calling the thing involves a
dereferencing, for which ->() is the syntax.
···
On Fri, Aug 25, 2006 at 09:18:39AM +0900, Matthew Johnson wrote:
There are languages (Scheme is one if I remember correctly) where the
two are semantically equivalent and thus () is used as a call
operator for both types of definitions. All functions / methods are
anonymous unless bound to a name. In such a language this would
indeed be a redefinition of the method as def is simply syntactic
sugar. This is approach is clean and simple in many respects, but is
different than the approach Ruby takes. Unless Ruby were to change
its semantics (not likely) I think David is right that this approach
won't work in Ruby.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky