1

In Ruby, on certain occasions (ruby/gosu/texplay) I'v seen code like the following used:

image.paint {
    circle 20, 20
    pixel 10, 10
}

Would it be possible for me to write a similar function that can be used like so?:

my_function {
    "one"
    "two"
    "three"
}

that would return and array: ["one", "two", "three"]

p.s. this function isn't just for generating arrays and I am not wondering about methods to do so, all I need to know about is the syntax. Thanks in advance, ell.

1 Answer 1

3

TexPlay looks nice, how do you find it? :)

I can't think of a way to do what you want, sorry. But if you prefix every string with _ it is easy:

function {
    _"one"
    _"two"
    _"three"
}

#=> ["one", "two", "three"]

Where:

def function(&block)
    Object.new.tap do |s| 
        s.instance_eval do            
            def _(var)
                @val ||= []
                @val << var
            end
        end
        s.instance_eval(&block)
     end.instance_variable_get(:@val)
end    
Sign up to request clarification or add additional context in comments.

1 Comment

I found it in the Gosu forums :) I use Gosu because its the simplest 2d library for Ruby I could find! Thanks for this example, its brilliant and tells me what I need to know. Thanks again

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.