map
のように、おそらく が必要です。
array = series.map do |num|
ただし、これを適切に使用するには、単に出力するのではなく、必要な配列の値を返すようにブロックを調整する必要があります ( return 、ここでは不要ですputs
) nil
。
の代わりに、メインの配列内のサブ配列に個々の数値を収集するにはputs
、必要に応じてsubarray.push( num )
、または同様の構文を使用します。これには、いくつかの同等の構文があります。次に、代わりに(または後)puts " "
、subarray
変数(またはそれを呼び出したもの)を最後に配置します。これにより、ブロックからの戻り値になり、メイン配列に追加されます。配列の配列になってしまいます。
Ruby オブジェクトで何ができるかを知りたい場合は、コマンドを起動しirb
て使用し、Ruby オブジェクトでmethods
できるすべてのことを確認することを強くお勧めします。
irb
1.9.3-p327 :001 > a = [1,2,3,4]
=> [1, 2, 3, 4]
1.9.3-p327 :002 > a.methods.sort
=> [:!, :!=, :!~, :&, :*, :+, :-, :<<, :<=>, :==, :===, :=~, :[], :[]=, :__id__, :__send__, :all?, :any?, :assoc, :at, :chunk, :class, :clear, :clone, :collect, :collect!, :collect_concat, :combination, :compact, :compact!, :concat, :count, :cycle, :define_singleton_method, :delete, :delete_at, :delete_if, :detect, :display, :drop, :drop_while, :dup, :each, :each_cons, :each_entry, :each_index, :each_slice, :each_with_index, :each_with_object, :empty?, :entries, :enum_for, :eql?, :equal?, :extend, :fetch, :fill, :find, :find_all, :find_index, :first, :flat_map, :flatten, :flatten!, :freeze, :frozen?, :grep, :group_by, :hash, :include?, :index, :initialize_clone, :initialize_dup, :inject, :insert, :inspect, :instance_eval, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, :instance_variable_set, :instance_variables, :is_a?, :join, :keep_if, :kind_of?, :last, :length, :map, :map!, :max, :max_by, :member?, :method, :methods, :min, :min_by, :minmax, :minmax_by, :nil?, :none?, :object_id, :one?, :pack, :partition, :permutation, :pop, :private_methods, :product, :protected_methods, :public_method, :public_methods, :public_send, :push, :rassoc, :reduce, :reject, :reject!, :repeated_combination, :repeated_permutation, :replace, :respond_to?, :respond_to_missing?, :reverse, :reverse!, :reverse_each, :rindex, :rotate, :rotate!, :sample, :select, :select!, :send, :shift, :shuffle, :shuffle!, :singleton_class, :singleton_methods, :size, :slice, :slice!, :slice_before, :sort, :sort!, :sort_by, :sort_by!, :taint, :tainted?, :take, :take_while, :tap, :to_a, :to_ary, :to_enum, :to_s, :transpose, :trust, :uniq, :uniq!, :unshift, :untaint, :untrust, :untrusted?, :values_at, :zip, :|]
それは学ぶべき方法の負荷と負荷です!しかし、たとえばRuby Array map
Google に入力すると、ほとんどの場合、最初の回答で役立つ参照が得られます。
Ruby のイディオムの多くは、配列メソッドとブロックを組み合わせて実行できることに基づいています。時間をかけて調べてみる価値はあります。