Do not understand this closure

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

a=square(11)

NoMethodError: undefined method `square' for main:Object
        from (irb):8

power 2

=> #<Proc:0x02dc58f0@(irb):2>

end

SyntaxError: compile error
(irb):10: syntax error
        from (irb):10

square 11

NoMethodError: undefined method `square' for main:Object
..futhermore...where (if it's implied...) is base come into play
here....

···

--
Posted via http://www.ruby-forum.com/\.

^ ^
             ^ ^

-a

···

On Fri, 25 Aug 2006, Dave Rose 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

a=square(11)

a=square[11]

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama

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.

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.

martin

No, mostly because () is not a method, it's syntax. on the other
hand is a method, and therefore available for Proc# to be aliased to
Proc#call

martin

···

On 8/24/06, rak rok <rakrok@gmail.com> 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?

harp:~ > cat a.rb
   def the_method() 'namespace one' end

   the_method = String.new 'namespace two'

   p the_method()
   p the_method

   harp:~ > ruby a.rb
   "namespace one"
   "namespace two"

that said, 1.9 has support for a_proc()

-a

···

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-

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dali lama

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?

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.

Hal

Martin DeMello 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?

No, mostly because () is not a method, it's syntax. on the other
hand is a method, and therefore available for Proc# to be aliased to
Proc#call

martin

oka...please explain for me where |base| parameter is comming
from...irb:

[4]power(5)

yntaxError: compile error
irb):9: syntax error
4]power(5)
       ^
       from (irb):9

end

yntaxError: compile error
irb):10: syntax error
       from (irb):10

4.power(5)
#<Proc:0x02dc58f0@(irb):2>
power(5).4

yntaxError: compile error
irb):12: no .<digit> floating literal anymore; put 0 before dot
ower(5).4
        ^
irb):12: syntax error
       from (irb):12

power(4)[4]
256
power(4)0.4

yntaxError: compile error
irb):14: syntax error
       from (irb):14

···

On 8/24/06, rak rok <rakrok@gmail.com> wrote:
power(5)[4]
1024

--
Posted via http://www.ruby-forum.com/\.

that said, 1.9 has support for a_proc()

Awesome -- I was wondering about that. Thanks.

-rr-

-a

···

--
to foster inner awareness, introspection, and reasoning is more efficient
than
meditation and prayer.
- h.h. the 14th dali lama

This used to work:

$ cat proc.rb
a = proc{|x| p x}
(a)(1)

... but doesn't anymore.

$ ruby19 -v proc.rb
ruby 1.9.0 (2006-08-13) [i686-linux]
proc.rb:2: warning: useless use of a variable in void context
proc.rb:2: syntax error, unexpected '(', expecting $end
(a)(1)

Right now, it's

RUBY_RELEASE_DATE # => "2006-08-13"
RUBY_VERSION # => "1.9.0"
a = proc{|x| x+1}
a.(2) # => 3

[gotta update my changelog]

···

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

  the_method = String.new 'namespace two'

  p the_method()
  p the_method

  harp:~ > ruby a.rb
  "namespace one"
  "namespace two"

that said, 1.9 has support for a_proc()

--
Mauricio Fernandez - http://eigenclass.org - singular Ruby

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.

-rr-

Hal

···

huh. so '()' is a method name now?

-a

···

On Fri, 25 Aug 2006, Mauricio Fernandez wrote:

that said, 1.9 has support for a_proc()

This used to work:

$ cat proc.rb
a = proc{|x| p x}
(a)(1)

... but doesn't anymore.

$ ruby19 -v proc.rb
ruby 1.9.0 (2006-08-13) [i686-linux]
proc.rb:2: warning: useless use of a variable in void context
proc.rb:2: syntax error, unexpected '(', expecting $end
(a)(1)

Right now, it's

RUBY_RELEASE_DATE # => "2006-08-13"
RUBY_VERSION # => "1.9.0"
a = proc{|x| x+1}
a.(2) # => 3

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

rak rok wrote:

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?

   def say
     puts "hello"
   end

   say = proc { puts "goodbye" }

   say() # hello or goodbye?

Cheers,
Hal

>>that said, 1.9 has support for a_proc()
>

[...]

>Right now, it's
>
>RUBY_RELEASE_DATE # => "2006-08-13"
>RUBY_VERSION # => "1.9.0"
>a = proc{|x| x+1}
>a.(2) # => 3

huh. so '()' is a method name now?

nope, it's just sugar standing for #call:

RUBY_VERSION # => "1.9.0"
RUBY_RELEASE_DATE # => "2006-08-13"
a = Object.new
def a.call(x); x+1 end
a.(2) # => 3

···

On Fri, Aug 25, 2006 at 03:50:31AM +0900, ara.t.howard@noaa.gov wrote:

On Fri, 25 Aug 2006, Mauricio Fernandez wrote:

--
Mauricio Fernandez - http://eigenclass.org - singular Ruby

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.

-rr-

Cheers,

···

Hal

hmmm. seems pointless. unless i can do

   a_method_or_proc(arg)

it one more chare than

   a_proc[arg]

   a_method[arg]

oh well, thanks for the info!

-a

···

On Fri, 25 Aug 2006, Mauricio Fernandez wrote:

huh. so '()' is a method name now?

nope, it's just sugar standing for #call:

RUBY_VERSION # => "1.9.0"
RUBY_RELEASE_DATE # => "2006-08-13"
a = Object.new
def a.call(x); x+1 end
a.(2) # => 3

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

Hi --

···

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.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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