Why does Array#each return an array with the same elements?

后端 未结 4 1874
名媛妹妹
名媛妹妹 2021-02-18 14:21

I\'m learning the details of how each works in ruby, and I tried out the following line of code:

p [1,2,3,4,5].each { |element| el }
4条回答
  •  忘了有多久
    2021-02-18 14:45

    If you want, for some reason, to suppress the output (for example debugging in console) here is how you can achive that

      [1,2,3,4,5].each do |nr|
        puts nr.inspect
      end;nil
    

提交回复
热议问题