block/yield puzzle 
Author Message
 block/yield puzzle

Hello --

Well, not puzzle in the fun sense -- just something I don't
understand.

I've been playing around with ways to generated a default block to
yield to.  This isn't a particular great way, but I'm stuck on why it
doesn't at least work.

  def thing(x, b = proc { puts "default" } )
    if block_given?
      yield
    else
      thing(x) &b
    end
  end

  thing(1) { puts "associated block" }  #  => "associated block"
  thing(1)                              #  infinite loop

Somehow the "x" argument seems to be causing trouble, because this
version does what I expected:

  def thing(b = proc { puts "default" } )
    if block_given?
      yield
    else
      thing &b
    end
  end

  thing{ puts "associated block" }  #  => "associated block"
  thing                             #  => "default"

I'm not seeing why the first version hangs.  Enlightenment welcome.

David

--
David Alan Black


Web:   http://www.*-*-*.com/ ~blackdav



Fri, 23 Apr 2004 03:36:02 GMT  
 block/yield puzzle
Quote:

>   def thing(x, b = proc { puts "default" } )
>     if block_given?
>       yield
>     else
        thing x, &b
>     end
>   end

>   thing(1) { puts "associated block" }  #  => "associated block"

    thing(1)                              #  => "default"


ruby 1.6.4 (2001-06-04) [i586-linux-gnu]

--
<[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]>
  )____(stud.techn.;ind.?k.data)||(softwareDeveloper.at(Trustix))_(
 /"Opinions expressed are mine and not those of my Employer,      "\
( "the University, my girlfriend, stray cats, banana fruitflies,  " )
 \"nor the frontal lobe of my left cerebral hemisphere.           "/



Fri, 23 Apr 2004 10:03:35 GMT  
 block/yield puzzle
A little more explanatory:

p = proc {|a| print " " + a.to_s }
def iter(x)
        yield x-1; yield x; yield x+1
end

iter(5,&p) #=> 4 5 6
iter(5) &p #=> LocalJumpError: yield called out of block

When you use the & to make a Proc back into a block, you seem to still
have to pass it along as a parameter to the function, you can't just add
it to the back like you normally do with an explicit block.

--
<[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]>
  )____(stud.techn.;ind.?k.data)||(softwareDeveloper.at(Trustix))_(
 /"Opinions expressed are mine and not those of my Employer,      "\
( "the University, my girlfriend, stray cats, banana fruitflies,  " )
 \"nor the frontal lobe of my left cerebral hemisphere.           "/



Fri, 23 Apr 2004 10:10:38 GMT  
 block/yield puzzle
Hello --

Quote:


> >   def thing(x, b = proc { puts "default" } )
> >     if block_given?
> >       yield
> >     else
> >       thing(x) &b
> >     end
> >   end

> >   thing(1) { puts "associated block" }  #  => "associated block"
> >   thing(1)                              #  infinite loop

> Is there anything wrong with this solution??
> def thing(x, b = proc { puts "default" } )
>   if block_given?
>     yield
>   else
>     thing(x, &b)
>   end
> end

> thing(1) { puts "associated block" }  #  => "associated block"
> thing(1)                              #  => "default"

> Works for me.

Nothing wrong with doing that; I just don't see why

   thing(x) &b

doesn't have the same effect.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Fri, 23 Apr 2004 04:11:38 GMT  
 block/yield puzzle

D>    thing(x) &b

 It's interpreted

      thing(x) & b

Guy Decoux



Fri, 23 Apr 2004 14:54:02 GMT  
 block/yield puzzle
..my 0.05$

Blocks cannot be instantiated and &b does not create the block you want.

In the expression 'thing(x) &b' '&b' is never evaluated. If it would be
evaluated, you'd get a 'parse error'.

But 'thing(x)' definitely goes into a recursion!

Clemens

Quote:
> -----Original Message-----

> Sent: Sonntag, 4. November 2001 21:12

> Subject: [ruby-talk:24344] Re: block/yield puzzle

Hello --

Quote:


> >   def thing(x, b = proc { puts "default" } )
> >     if block_given?
> >       yield
> >     else
> >       thing(x) &b
> >     end
> >   end

> >   thing(1) { puts "associated block" }  #  => "associated block"
> >   thing(1)                              #  infinite loop

> Is there anything wrong with this solution??
> def thing(x, b = proc { puts "default" } )
>   if block_given?
>     yield
>   else
>     thing(x, &b)
>   end
> end

> thing(1) { puts "associated block" }  #  => "associated block"
> thing(1)                              #  => "default"

> Works for me.

Nothing wrong with doing that; I just don't see why

   thing(x) &b

doesn't have the same effect.

David

--=
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Fri, 23 Apr 2004 15:02:21 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. block.call vs. yield

2. block , yield and proc?

3. yield works, but no block given

4. "yield called out of block"

5. yield works, but no block given

6. Block Puzzle

7. std blocks vs blocks+cache ( was: block behavior)

8. question on return from blocks and ensure: blocks

9. ERROR "Corrupt Block/Unknown Block Freed"

10. Corrupt block/unknown block type freed bug

11. 0 BLOCK in Gforth (Was: Blocks Help etc.)

12. blocks and lambdas, or blocks as first-class entities

 

 
Powered by phpBB® Forum Software