1

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.

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

Ok, not answering the exact question, and I imagine you and dirk know this, but...

In case someone wants an Array as a parameter and their search has led them here, Ruby does have a basic feature that does something similar for you:

def f *x
  x
end

p f :foo,
    :bar,
    :foobar 

# => [:foo, :bar, :foobar]

Comments

0

Though the syntax you suggested will not pass a parser, there are some tweaks:

def sequence(&block)
  yield
end

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.