This has to be a FAQ, but after much web searching, I've failed to find an answer. I'm new to the Ruby programming language, so it's possible that I've misunderstood something fundamental, or that I'm projecting my knowledge of C#, F#, or Haskell onto Ruby.
If I have a method like the following, is there a one-liner to turn it into an array or other mutable data structure?
def foo
yield 8
yield 2
yield 5
end
I know that I can do this:
ary = Array.new
foo { |x| ary << x }
That is, however, not a one-liner. In C#, for example, I can use ToList or ToArray extension methods, or in F# e.g. Seq.toList:
let foo = seq {
yield 8
yield 2
yield 5
}
let arr = foo |> Seq.toList
Is there a similar one-liner in Ruby?
Or did I completely misunderstand the nature of foo?