1

I have a bit of Ruby that outputs correctly as a .rb file. However, I want to include equivalent code in a HAML file, and I just can't get it to output correctly. I've tried to reformat it in various ways, but haven't been able to figure it out, nor find exactly what I'm looking for in another question.

I'm generating repeated div blocks, using elements stored in an array of arrays. There are groups of 3 .class objects that I want to group under a .row. I've got to make a BUNCH of these, so I want to automate this if possible. Right now I can run the ruby code in sublime text, and then copy+paste the formatted HAML into my HAML page... but it seems like I should be able to do it in-line!

Here's the Ruby code:

gfys = [
  [ #ROW 1
    {"Title"=>"MOVE", "ID"=>"#gfy0a", "Desc"=>"Use either analog stick or directional buttons."},
    {"Title"=>"SHOOT", "ID"=>"#gfy0b", "Desc"=>"Press any button; pull any trigger."},
    {"Title"=>"PUSH", "ID"=>"#gfy0c", "Desc"=>"Projectiles displace the ball on impact."}
  ],
  [ #ROW 2
    {"Title"=>"AIM", "ID"=>"#gfy1a", "Desc"=>"Projectiles are triangular. Balls are circular."},
    {"Title"=>"LAUNCH", "ID"=>"#gfy1b", "Desc"=>"Shoot while moving for faster projectiles."},
    {"Title"=>"SCORE", "ID"=>"#gfy1c", "Desc"=>"Push a ball into the opposing goal to earn a point."}
  ]
]

gfys.each do |group|
  puts "// ROW #{gfys.index(group)}"
  puts ".row"
  group.each do |gfy|
    puts "\t.class
    .class2
      %h2
        #{gfy["Title"]}
      #{gfy["ID"]}.class
      %p
        #{gfy["Desc"]}"
  end
end

This outputs correctly to the HAML code:

// ROW 0
.row
  .class
    .class2
      %h2
        MOVE
      #gfy0a.class
      %p
        Use either analog stick or directional buttons.
  .class
    .class2
      %h2
        SHOOT
      #gfy0b.class
      %p
        Press any button; pull any trigger.
  .class
    .class2
      %h2
        PUSH
      #gfy0c.class
      %p
        Projectiles displace the ball on impact.
// ROW 1
.row
  .class
    .class2
      %h2
        AIM
      #gfy1a.class
      %p
        Projectiles are triangular. Balls are circular.
  .class
    .class2
      %h2
        LAUNCH
      #gfy1b.class
      %p
        Shoot while moving for faster projectiles.
  .class
    .class2
      %h2
        SCORE
      #gfy1c.class
      %p
        Push a ball into the opposing goal to earn a point.

Now, just running the code under a :ruby filter in the HAML file doesn't work for me. It seems to print the whole array into the page, and then everything else gets goofy. Taking out the END tags, and accounting for HAML's attempts to "simplify" the code don't work. Putting - or = characters as appropriate before the code hasn't worked for me.

I know the situation is complicated by my formatting in the array, but collapsing the array to a single line didn't help, either.

Finally, I'm pre-compiling this before uploading it to a page, using Hammer.app, if that matters.

Is what I'm trying crazy? Or what am I missing? I really appreciate your help!

  • is this for a Rails app? its tagged as Rails but your question seems to indicate its just a ruby app outputting to STDOUT – engineerDave Mar 29 '15 at 03:27
  • @engineerDave I tagged it with Rails because it might be applicable in a Rails context. But no, I guess I'm not working with Rails at the moment. Should I remove the Rails tag? This is my first actual SO question. :) – Nick Splendorr Mar 30 '15 at 04:24
  • well it changes the scope on what you're trying to accomplish, i.e. libraries you are using/have available. I don't think you need to remove it just indicate in your Question your setup and what you're trying to do – engineerDave Mar 30 '15 at 17:59
  • @engineerDave Good point. I removed the ruby-on-rails tag, since it isn't appropriate here. – Nick Splendorr Mar 31 '15 at 21:00
  • Also I would change that multiline puts to a HERE doc http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html you can interpolate within a HERE doc in the normal way #{ruby code here} – engineerDave Apr 01 '15 at 20:48
  • thanks @engineerDave, I don't think I've ever seen this before. – Nick Splendorr Apr 03 '15 at 00:51

0 Answers0