Why does ary.each unload the entire contents of an object?

foo is an array of objects, bar is an attribute of this object.

(rdb:1) foo.bar.map{|v| bar.v }
["a", "b", "c", "d", "e", "f"]


(rdb:1) foo.bar.each{|v| p bar.v }
[massive outpouring of object attributes]
+3
source share
3 answers

Because the result is eachdefined as a renamed Enumerable.

If you want to use eachin irb and not boot with output, then:

foo.bar.each{|v| p bar.v }; nil
+6
source

#eachwill return its receiver, and then irb decides to print it, since irb is a REPL.

You can simply click .any?at the end of the expression:

(rdb:1) foo.bar.each{|v| p bar.v }.any?
# output from only the #p call
=> true
+1
source

, IRB . , ; nil .

inspect bars, .

class Bar
  def inspect
    "tl;dr"
  end
end
0
source

All Articles