Need help improving Ruby DSL to control an Arduino-controlled drink (monkey bar)

I am writing DSL in Ruby to manage the Arduino project I am working on; Bardino. This is a monkey bar, which will be controlled by software for serving drinks. The Arduino receives commands through the serial port to tell the Arduino which pumps to turn on and for how long.

He is currently reading the recipe (see below) and printing it back. The code for serial communication has yet to be processed, as well as some other ideas that I talked about below.

This is my first DSL, and I am working on the previous example, so it is very rough around the edges. Any criticisms, code improvements (are there any good links to Ruby DSL best practices or idioms?), Or any general comments.

I currently have a draft DSL, so the drink recipe is as follows ( Github link ):

desc "Simple glass of water" recipe "water" do ingredients( "Water" => 2.ounces ) end 

This, in turn, is interpreted and currently leads to the following ( Github link ):

 [ mwilliams@Danzig barduino-tender]$ ruby barduino-tender.rb examples/water.rb Preparing: Simple glass of water Ingredients: Water: 2 ounces 

This is a good start for DSL, but I think it can be implemented a little better. Some ideas that I had below:

  • Identify available “ingredients” using the name of the ingredient and the number of pumps to which it is connected. Maybe use a hash? ingredients = {"water" => 1, "vodka" => 2}. Thus, when an ingredient is interpreted, it can either: a) send the pump number on the serial port, followed by the number of ounces for the Arduino, to discard it; b) inform the user that this ingredient does not exist and is interrupted, so nothing is released; c) they may easily be able to modify or add new ingredients if modified.
  • How to make a recipe less like code, which is the main goal of DSL, maybe build a recipe builder? Using available ingredients to offer the user the name of the drink, the ingredients and how much?

The Github project is here , feel free to fork and make pull requests, or post your suggestions and code examples here to other users. see. And if you're curious at all, Arduino code using the Ruby Arduino Development framework is here .

Update

I changed and cleaned things up a bit to reflect Orion Edwards' offer for the recipe. Now it looks as follows.

 description 'Screwdriver' do serve_in 'Highball Glass' ingredients do 2.ounces :vodka 5.ounces :orange_juice end end 

I also added a hash (the key, which is the ingredient and the value of the number of the pump to which it connected). I think this has made significant progress. I will leave the question open for any further suggestions, but in the end we will choose Orion's answer. Updated DSL code here .

+6
ruby language-design dsl arduino
source share
3 answers

Without studying implementation details (or your github links), I would try to write DSL as follows:

(theft from here: http://supercocktails.com/1310/Long-Island-Iced-Tea- )

 describe "Long Island Iced Tea" do serve_in 'Highball Glass' ingredients do half.ounce.of :vodka half.ounce.of :tequila half.ounce.of :light_rum half.ounce.of :gin 1.dash.of :coca_cola #ignoring lemon peel as how can a robot peel a lemon? end steps do add :vodka, :tequila, :light_rum, :gin stir :gently add :coca_cola end end 

Hope this helps!

+5
source share

If you want the recipe to look more natural, why not (from the same Orion Ewards recipe, thanks!):

 Recipe for Long Island Iced Tea #1 Ingredients: 1/2 oz Vodka 1/2 oz Tequila 1/2 oz Light Rum 1/2 oz Gin 1 Dash Coca-Cola # ignored Twist of Lemon Peel (or Lime) 

Then add Treetop to the mix. You may have rules such as:

 grammar Cocktail rule cocktail title ingredients end rule title 'Recipe for' S text:(.*) EOF end rule ingredients ingredient+ end rule ingredient qty S liquid end # ... end 

Which treetop compiler is converted to a good ruby ​​module. Then:

 parser = CocktailParser.new r = parser.parse(recipe) 
+3
source share

Orion DSL looks very beautiful. The only change I could suggest from your “updated” code is

  • Replace description with recipe . This is a more descriptive term.
  • Since the set of ingredients and actions is fixed, associate the ingredients with variables, not symbols, that is, you have vodka = :vodka some place defined. Easier said

    mix vodka, gin and triple_se # instead of using: vodka ,: gin and: triple_sec.

anyway, that junior nit.

+1
source share

All Articles