なんとなくyieldのイメージがつかめた気がする
すげぇ便利くさい。
# Array#eachの(粗製乱造の)類似品 def iich(arr) # 引数に配列を取る idx = 0 while idx < arr.size yield(arr[idx]) # 引数の各要素毎に、その要素を引数にしてブロックを起動 idx += 1 end end
たとえば
StructA = Struct.new("StructA",:a,:b,:c) class StructB < StructA def each_elem e = %w(a b c) i = 0 e.each do |elem| yield(elem,self.send(elem)) end end end test = StructB.new test.a = "111" test.b = "222" test.c = "333" test.each_elem do |key,data| p "#{key}::#{data}" end #=> "a::111" "b::222" "c::333"
自分的にキタコレ