def map(array,&block)
new_array = []
array.each do |item|
new_array << block.call(item)
end
new_array
end
array_block = Proc.new { | item | item.to_i}
array = [1,2,3]
result = map(array,&array_block)
p result
module Hoge
def hoge
p "hoge method"
end
module_function :hoge
end
class Fuga
include Hoge
Hoge.hoge
end
g = Fuga.new
hoge #=> "NilClass"
hoge {} #=> "Proc"
引数なしはnil {}でProcとして渡される。
def hoge(&block)
block.call
end
hoge{p "fuga"}
出題内容が理解できなかった。
module A
end
class B
include A
end
value = B.new
x = B.new
p value.class===x.class
p value.class==x.class
p x.class
=begin
value = String.new
x = String.new
=end
case value
when x
p "x"
else
p "other"
end
if value.is_a?(x.class)
p "x"
else
p "other"
end
- 等しくなる時
- x:
- value:
- ならない時
- x: クラスBのインスタンス
- value: クラスBのインスタンス
等しくなる時、ならない時の違いがいまいち納得出来ていない。
def change_instance_variables(&block)
begin
@b = "c"
block.call
@b = "b"
rescue
@b = "b"
end
end