Easy dsl style method generation?

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

Roger P. wrote:

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.

At 2009-12-03 10:38AM, “Roger P.” wrote:

end

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

My spidey-sense tells me you’re trying to do something like this:

class Move
  def move(from, to)
    if @location == from
      @location = to
    else
      raise "I'm not located at #{from}!"
    end
  end
end

m.move(x, y)

Multi-word methods can be found in Smalltalk, but not in Ruby.

Another possibility that makes calling your method more readable:

class Move
  def move(coords)
    if @location == coords[:from]
      @location = coords[:to]
    else
      raise "I'm not located at #{coords[:from]}!"
    end
  end
end

m.move(:from => x, :to => y)