Search code examples
rubylambdablockproc

Ruby Block to Array


I'm trying to take a block argument in a method, and turn the contents (array of Symbols) and make it into an array. For example:

def sequence(&block)
  # ?
end

sequence do
  :foo,
  :bar,
  :foobar
end # => [:foo, :bar, :foobar]

I know it would be easier to just have an array as an argument in the sequence method, but I'm trying to stay consistent with another method.


Solution

  • That syntax is not allowed in Ruby, so unfortunately that is impossible since the code won't even make it past the parser.