[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

do not understand this closure

dave rose

8/24/2006 5:52:00 PM

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

21 Answers

Ara.T.Howard

8/24/2006 5:56:00 PM

0

Martin DeMello

8/24/2006 5:57:00 PM

0

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

Martin DeMello

8/24/2006 6:17:00 PM

0

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?

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

Ara.T.Howard

8/24/2006 6:27:00 PM

0

dave rose

8/24/2006 6:31:00 PM

0

Martin DeMello wrote:
> 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?
>
> 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
> power(5)[4]
> 1024

--
Posted via http://www.ruby-....

Mauricio Fernández

8/24/2006 6:44:00 PM

0

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()

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]
--
Mauricio Fernandez - http://eige... - singular Ruby

Ara.T.Howard

8/24/2006 6:51:00 PM

0

Mauricio Fernández

8/24/2006 7:13:00 PM

0

On Fri, Aug 25, 2006 at 03:50:31AM +0900, ara.t.howard@noaa.gov wrote:
> On Fri, 25 Aug 2006, Mauricio Fernandez wrote:
>
> >>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

--
Mauricio Fernandez - http://eige... - singular Ruby

Ara.T.Howard

8/24/2006 7:28:00 PM

0

Hal E. Fulton

8/24/2006 11:15:00 PM

0

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