最近Rubyの学習を始めました。しかし、私はちょうど私が理解していない事件を発見した:それぞれに渡されたコードブロックは角括弧で処理されますが、 'do' - 'end'(ruby)では使用できません
#my_hash is a hash in which the keys are strings and the values arrays, but dont think about the specifics fo the code
#if I run my code like this, it works perfectly
my_hash.each do |art|
puts mystring.gsub(art[0]).each {
art[1][rand(art[1].length) -1]
}
end
#but if I use this, it prints "Enumerator"
my_hash.each do |art|
puts mystring.gsub(art[0]).each do
art[1][rand(art[1].length) -1]
end
end
それはあなたが巣の終わりの対エンドペアはありますか? 私はここで
答えはありませんが、あなたはハッシュイテレータを '| k、v |'形式で単純化することができます。 –
これは[Ruby Block Syntax Error](http://StackOverflow.Com/q/6854283/)、[ブロック定義 - 中括弧と 'do'-' end'の違いは何ですか?](http:// StackOverflow '' do'ブロックと '' {'} ''を使用して、' 'do''を実行していないRubyマルチラインブロック(http://StackOverflow.Com/q/3680097/) http://StackOverflow.Com/q/2122380/)、[Rubyでこれらのブロックコーディングスタイルの違いや価値は何ですか?](http://StackOverflow.Com/q/533008/)、[Rubyブロックと括弧をはずさない引数](http://StackOverflow.Com/q/420147/)。 –