easy dsl style method generation?

R

Roger Pack

Here's what I wish I could easily do.

Move.from(x).to(y)

and set it up easily, like

class Move
def from(x).to(y)

end
end

Any helpers out there to make this somewhat possible that anyone knows
of?

Much thanks.
-r
 
A

Aldric Giacomoni

Roger said:
Here's what I wish I could easily do.

Move.from(x).to(y)

and set it up easily, like

class Move
def from(x).to(y)

end
end

Apparently, what you're looking for is something like this:

class Move
def from x
@x = x
return self
end

def to y
raise ArgumentError, "X is not defined!" unless @x
@y = y
# Do your actual move
end
end

In plain english, "from" returns an object on which you can call "to".
I mean, I would just do "def move_from_to x, y" but maybe your way can
be done.. Have fun with the error checking though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top