CascadingRubies

4 views
Skip to first unread message

Tim Morgan

unread,
Jun 2, 2009, 11:05:07 PM6/2/09
to tul...@googlegroups.com
I just put some code up on GitHub I'd like to get some feedback on...

http://github.com/seven1m/cascading_rubies

Basically, it's a Ruby DSL that allows you to write CSS fairly easily.
Here's an example:

header {
background_color '#eee'
margin_bottom '10px'
nav {
border '1px solid #000'
a(:link, :active, :visited) { color 'blue' }
a(:hover) { color 'red' }
}
div.search {
float 'right'
}
}

Output:

#header { background-color: #eee; margin-bottom: 10px; }
#header #nav { border: 1px solid #000; }
#header #nav a:link, #header #nav a:active, #header #nav a:visited {
color: blue; }
#header #nav a:hover { color: red; }
#header div.search { float: right; }


This started as an experiment, and turned out to be something I think
could be useful. Of course, there are some (documented) shortcomings.
Just wondering what you guys think. Would you use something like this?
Does it need a Rails plugin?

Thanks.

Oh and BTW, we have a meeting planned for June 15th. Is that a good
date for everyone? Any ideas for presentations?

-Tim

John Hornbeck

unread,
Jun 2, 2009, 11:09:09 PM6/2/09
to tul...@googlegroups.com
do you see an advantage of this over SASS? Just curious of your reasoning behind doing it yourself as opposed to using a very popular library that already does something like this.
--
John Hornbeck
Support Manager
Engine Yard
http://engineyard.com

“Work hard to find something that fascinates you.” - Richard Feynman

Justin Richter

unread,
Jun 2, 2009, 11:55:34 PM6/2/09
to tul...@googlegroups.com
It looks simple enough, but I have to echo Hornbeck's post.  One thing I like about SASS is that you don't have to use curly brackets.

Justin Richter

unread,
Jun 3, 2009, 12:03:40 AM6/3/09
to tul...@googlegroups.com
What made you want to create it?  Just curious. 

Joseph A Holsten

unread,
Jun 3, 2009, 12:08:11 AM6/3/09
to tul...@googlegroups.com
I think I like the spirit of this over SASS for the same reason I like
markaby/builder over haml. It's a ruby internal dsl, not an external
dsl with a new syntax. Clearly it's not as complete as sass, but I'd
feel more at home extending this to do crazy stuff.

Justin: there's always do ... end; :)

John Hornbeck

unread,
Jun 3, 2009, 12:12:29 AM6/3/09
to tul...@googlegroups.com
I'm not following what you mean by internal vs. external dsl.  Do explain

Justin Richter

unread,
Jun 3, 2009, 12:21:40 AM6/3/09
to tul...@googlegroups.com
:)

On Tue, Jun 2, 2009 at 11:08 PM, Joseph A Holsten <jos...@josephholsten.com> wrote:

Justin Richter

unread,
Jun 3, 2009, 12:23:19 AM6/3/09
to tul...@googlegroups.com
I too like the simplicity, it seems easily hackable.

Joseph A Holsten

unread,
Jun 3, 2009, 1:06:50 AM6/3/09
to tul...@googlegroups.com
Fowler has the canonical definitions: http://martinfowler.com/bliki/DomainSpecificLanguage.html

Internal dsls implement domain specific features entirely within a
host language. Think of them more like code than data. They're a bit
like tricked out libraries. They use the host language to parse the
dsl. They're quite popular in with TCL, lisp and ruby. examples:
markaby, rails (esp migrations), rake, emacs lisp, guile, macports
portfiles, .exrc, .shrc.

External dsls require a custom parser. Think of them more like data
than code. They're popular with unix, .net, java. examples: Ant, /etc/
passwd, csv, vcard, yaml configs, xml configs. Arguably every markup
language lies in here.

John Hornbeck

unread,
Jun 3, 2009, 1:10:55 AM6/3/09
to tul...@googlegroups.com
Ahh, makes sense now :-)

> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups "Tulsa Ruby User Group" group.
> To post to this group, send email to tul...@googlegroups.com
> To unsubscribe from this group, send email to tulsarb+u...@googlegroups.com
> For more options, visit this group at http://groups.google.com/

Tim Morgan

unread,
Jun 3, 2009, 10:04:08 AM6/3/09
to tul...@googlegroups.com
Thanks Joseph for that wonderful explanation. Yes, this library is a
bonefied DSL, meaning the code is actual Ruby. Sass, on the other
hand, is interpreted as it's own language. With the DSL approach, I'm
able to mix in regular Ruby variables, require Ruby libraries, do
color arithmetic, heck I could even pull CSS colors and font sizes out
of my database and write them directly in my CSS file.

As for why I did it... because I can. Isn't that the best reason of
all? :-) No really, it was just an experiment, and turned out to be
less than 100 lines of code. And I figure it might be useful as a part
of OneBody or another project someday.

Thanks for your feedback!

Tim Morgan

unread,
Jun 3, 2009, 10:11:42 AM6/3/09
to tul...@googlegroups.com
Let me clarify: CascadingRubies is an internal Ruby DSL -- not an
external interpreted one like Sass. Cuz I said that kinda funny.

Tanner Burson

unread,
Jun 3, 2009, 10:37:00 AM6/3/09
to tul...@googlegroups.com
Personally I dislike all the crazy CSS DSLs, and I actually tend to use some simple ERB templates to generate CSS instead of a full blown CSS DSL.  But o stand up in defense of SASS. 

The advantage to an external DSL is that you don't HAVE to know a lot of details about the hosting language to be able ti use the DSL.  While Tim's code is pretty slick, it suffers from a few issues, some easy to fix, others not. 

The first being because you don't inherit from a BlankSlate (or similar) base class, any method available on the Object class cannot be used as a selector name without using the s() method.  Try using the name 'id' as a selector.  As mentioned this is somewhat easier to fix by using BlankSlate or something similar to undefine all the default methods.

The second is that you can't use any ruby keywords as selectors.  As long as you use instance_eval to run the raw code without doing any sort of parsing this will remain true. 

Obviously those aren't a big deal if you know Ruby, and are aware of the limitations.  But it does come with it's own set of "gotchas" that an external DSL doesn't have to worry about.

All of that said, I've already forked this and look forward to tinkering with it!

--Tanner Burson
--
===Tanner Burson===
tanner...@gmail.com
http://www.tannerburson.com

Tim Morgan

unread,
Jun 4, 2009, 1:03:25 AM6/4/09
to tul...@googlegroups.com
OK, I just released 0.2.0 of my gem. This version addresses a few of
the shortcomings the library had with version 0.1.0.

* Use of BlankSlate and other tricks to clean up the instance_eval
space and avoid
stepping on potential selectors and rules.
* Easily generate comma-separated selectors via the DSL.
* New optional block syntax for rcss files.

Thanks so much for your valuable feedback everyone.

Joseph A Holsten

unread,
Jun 5, 2009, 2:33:07 AM6/5/09
to tul...@googlegroups.com
That's a great point, and I'm sure there's a bunch of other standard
internal DSL issues that I don't even know to check for. I'm kinda
amazed there isn't a 'Building DSLs in Ruby' book.
has anyone gone through this:
http://obiefernandez.com/presentations/obie_fernandez-agile_dsl_development_in_ruby.pdf
Seems like cascading rubies is a straightforward instatiation dsl, but
there's some other nifty examples in there too. I also learned some
tricks from:
http://www.scribd.com/doc/12709855/Advanced-DSLs-in-Ruby
esp, const factories, instance_eval'ing, the fuzzy line between
internal & external. Neal ford seems to have quite a few good presos
on dsls if you need some google fu.

Tanner Burson

unread,
Jun 5, 2009, 10:26:15 AM6/5/09
to tul...@googlegroups.com
Great resources Joseph, thanks!

Joseph A Holsten

unread,
Jun 5, 2009, 7:46:05 PM6/5/09
to tul...@googlegroups.com
I forgot about a book I've been interested in, “Building Domain
Specific Languages in Boo,” by Ayende Rahien[1]. That's the guy who
wrote RhinoMock for c#. (If you've got to use .NET, you absolutely
need to know LINQ and RhinoMock to know the furthest edge of the
possible in the language.)

Do any of the local user groups have a relationship with Manning to
get a copy?

1: http://www.manning.com/rahien/
Reply all
Reply to author
Forward
0 new messages