0a9428093c
git-subtree-dir: RubyFun git-subtree-mainline:a04a502787
git-subtree-split:b01c682613
31 lines
635 B
Ruby
31 lines
635 B
Ruby
require 'pp'
|
|
|
|
def call_twice
|
|
puts "Calling your block."
|
|
ret1 = yield("very first")
|
|
puts "The value of your block: #{ret1}"
|
|
|
|
puts "Calling your block again."
|
|
ret2 = yield("second")
|
|
puts "The value of your block: #{ret2}"
|
|
end
|
|
|
|
call_twice do |which_time|
|
|
puts "I'm a code block, called for the #{which_time} time."
|
|
which_time == "very first" ? 1 : 2
|
|
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}" }
|