more in 7.2

This commit is contained in:
Dan Buch 2011-10-06 22:24:01 -04:00
parent 697fc60bcb
commit 2d3af8dd32

View File

@ -1,3 +1,5 @@
require 'pp'
def call_twice def call_twice
puts "Calling your block." puts "Calling your block."
ret1 = yield("very first") ret1 = yield("very first")
@ -12,3 +14,17 @@ call_twice do |which_time|
puts "I'm a code block, called for the #{which_time} time." puts "I'm a code block, called for the #{which_time} time."
which_time == "very first" ? 1 : 2 which_time == "very first" ? 1 : 2
end end
class Hash
def find_all
new_hash = Hash.new
each { |k,v| new_hash[k] = v if yield(k, v) }
new_hash
end
end
squares = {0=>0, 1=>1, 2=>4, 3=>9}
print "squares = "
pp squares
(squares.find_all { |key, value| key > 1 }).each { |k,v| puts "#{k}: #{v}" }