Forum: Ruby how to write a DSL

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5cabbcab339feace80762194dc9302ed?d=identicon&s=25 Christoph Jasinski (Guest)
on 2009-06-09 13:12
(Received via mailing list)
Hi there,
I'm new to DSLs and wanted to know if someone could tell me some good
and
deep sources (books, long articles, examples, tutorials, etc.) for how
to
write DSLs with Ruby. Of course you get some hits with Google, but I
can't
distinct between good and bad ones. If you already delt with it, please
give
me a hint.

I've seen something which Shoulda is capable of: (that's a kind of a
DSL,
right?)

class RubyMagic < Test::.....
.
.
.
  should "be a minor if younger than 18" do
    .
    this and that
    .
  end
.
.
.
end

How do you write things like this. I wanted to let the user say
something
like this and then proceed his/her request. Is it possible?Is it not
that
difficult?


Thanks,

Chris
B3881a28fe402dd2d1de44717486cae8?d=identicon&s=25 Michael Kohl (Guest)
on 2009-06-09 14:26
(Received via mailing list)
On Tue, Jun 9, 2009 at 1:11 PM, Christoph
Jasinski<christoph.jasinski@googlemail.com> wrote:
> I'm new to DSLs and wanted to know if someone could tell me some good and
> deep sources (books, long articles, examples, tutorials, etc.)

http://onestepback.org/articles/lingo/index.html
http://www.infoq.com/presentations/vanderburg-stat...
http://www.infoq.com/presentations/5W-of-DSL

If you are the type of person who learns well by reading source, you
could have a look at Sinatra:

http://github.com/sinatra/sinatra/tree/master

Michael
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-09 14:28
Christoph Jasinski wrote:
> I've seen something which Shoulda is capable of: (that's a kind of a
> DSL,
> right?)
>
> class RubyMagic < Test::.....
> .
> .
> .
>   should "be a minor if younger than 18" do
>     .
>     this and that
>     .
>   end
> .
> .
> .
> end
>
> How do you write things like this. I wanted to let the user say
> something
> like this and then proceed his/her request. Is it possible?Is it not
> that
> difficult?

I'd suggest that a good starting point is to install shoulda and look at
its source code.

Note that

   should "be a minor" do
     stuff
   end

is just a regular method call with a block. Try this:

def should(*args)
  puts "Got args: #{args.inspect}"
  if block_given?
    puts "Got a block"
    yield
  end
end

should "be a minor" do
  puts "I am here"
end
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-06-09 14:35
(Received via mailing list)
Christoph Jasinski <christoph.jasinski@googlemail.com> writes:
> I'm new to DSLs and wanted to know if someone could tell me some good and
> deep sources (books, long articles, examples, tutorials, etc.) for how to
> write DSLs with Ruby. Of course you get some hits with Google, but I can't
> distinct between good and bad ones. If you already delt with it, please give
> me a hint.

While I don't always agree with the details written in
http://martinfowler.com/dslwip/
(notably concerning Lisp), I find that presentation quite exhaustive and
interesting.

After reading it, you should be able to write any kind of DSL in any
programming language...
5cabbcab339feace80762194dc9302ed?d=identicon&s=25 Christoph Jasinski (Guest)
on 2009-06-09 14:41
(Received via mailing list)
Thank's guy, looks really cool.
The Pragmatic Programmers have also this here:
Language Design Patterns: Techniques for Implementing Domain-Specific
Languages by Terence Parr

 This also could be interesting

Anyway, thanks again.
Cheers,

Chris
9274b07a5c4eab92c7cc9a25cdc17278?d=identicon&s=25 Stefan Zimmermann (Guest)
on 2009-06-09 14:54
(Received via mailing list)
Am 09.06.2009 14:40 Uhr, schrieb Christoph Jasinski:

> The Pragmatic Programmers have also this here:
> Language Design Patterns: Techniques for Implementing Domain-Specific
> Languages by Terence Parr

And in "Design Patterns in Ruby" by Russ Olsen is also an own chapter
about DSLs; quite good (IMHO).


Stefan
This topic is locked and can not be replied to.