[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:03978] Associating block to block call?

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-07-13 20:10:10 UTC
List: ruby-talk #3978
What are my chances to associate block to a block call? 

The following example does not call any of associated blocks:

block = proc {
  # |&bl| # causes parse error
  |bl|
  puts "in block"
  puts("call passed: "+bl.call) if bl
  puts(yield) if iterator?
  "return"
}

puts "1)"
puts block.call
puts

puts "2)"
puts block.call do
  puts "associated iterator"
end
puts

puts "3)"
puts block.call( proc{"passed proc object"} )
puts

def block_assoc(block, &associated_block)
  puts "4)"
  associated_block.call
  puts block.call
end

def block_assoc2(block)
  puts "5)"
  yield
  puts block.call
end


block_assoc( block ) do
  puts "testing associated blocks"
end
puts

block_assoc2( block ) do
  puts "testing associated blocks"
end

################
Outputs:
1)
in block
return

2)
in block
return

3)
in block
call passed: passed proc object
return

4)
testing associated blocks
in block
return

5)
testing associated blocks
in block
return

#####################

This is a example demonstrates the effect of context. The block2 yields
without reason.

block1 = proc { 
  puts "block1"
  yield if iterator?     # should't be yielded
}
def block2
  proc { 
    puts "block2"
    yield if iterator?   # should't be yielded
  }
end

block1.call
block2.call

block1
block2
block_iterator_2.rb:8:in `block2': yield called out of iterator
(LocalJumpError)
        from block_iterator_2.rb:7:in `call'
        from block_iterator_2.rb:13


	- Aleksi

In This Thread

Prev Next