# Easy dsl style method generation?

**URL:** <https://www.ruby-forum.com/t/easy-dsl-style-method-generation/179778>\
**Category:** Ruby\
**Created:** [December 3, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/easy-dsl-style-method-generation/179778 "2009-12-03T16:38:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 3, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/easy-dsl-style-method-generation/179778/1 "2009-12-03T16:38:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 3, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/easy-dsl-style-method-generation/179778/2 "2009-12-03T17:15:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [December 3, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/easy-dsl-style-method-generation/179778/3 "2009-12-03T18:05:53Z")

</div>

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)
```
