ブロックを使用して約と呼んでこの練習問題を実行すると、と表示されます。それは次のようになります。Ruby:ブロックと利回り
["Christopher Alexander", "John McCarthy", "Emperor Joshua Abraham Norton"]
私は、一般的にブロックし、歩留まりを使用する方法を知っている:
class Hero
def initialize(*names)
@names = names
end
def full_name
# a hero class allows us to easily combine an arbitrary number of names
# this is where yield should be
end
end
def names
heroes = [Hero.new("Christopher", "Alexander"),
Hero.new("John", "McCarthy"),
Hero.new("Emperor", "Joshua", "Abraham", "Norton")]
# I have to use #map and #join to unify names of a single hero
end
戻り値は次のようになります。私はこれの前に非常に簡単な演習をしましたが、私はこの問題を解決することはできません。
ありがとうございました=) – Barbared