Search code examples
rubyhamlmiddleman

Nested methods in Ruby Helper


I'm working on a project in MiddleMan. Trying to pass the results of a method as the parameters of another method. I'm running into all kinds of syntax issues with the commas and parens. Everything looks right to me, am I crazy? Thanks in advance!

The method call that I'm having trouble with is create_campus_subnav()

module SubnavHelper

  def create_subnav_link ( label, link="" )
    if link == ""
      link = label
    end
    return "          <li id='item-#{link}'><a class='title' id='link-#{link}'>#{label}</a></li>"
  end

  def create_subnav (*links)
    subnav = "\
    <nav class='nav-sub'>\
      <div class='nav-wrap'>\
        <ul class='nav-list'>"

    links.each do |attr,val|
      subnav += " attr: " + attr;
      subnav += " val: " + val;
      # subnav += args["attr"] + "\n"
    end 

    subnav += "\
        </ul>\
      </div>\
    </nav>"
    return subnav
  end

  def create_campus_subnav
      subnav = create_subnav ( 
        create_subnav_link ("Campus Overview", "Overview"), 
        create_subnav_link ("About", "About"),  
        create_subnav_link ("Admissions"), 
        create_subnav_link ("Career Services" "CareerServices"),       
        create_subnav_link ("Schedules, Tuition & Fees" "Tuition"),         
        create_subnav_link ("Financial Assistance & Scholarships" "FinancialAssistance"),         
        create_subnav_link ("Faculty")      
      )
      puts subnav
  end
end

And the errors:

/dev/config.rb:54:in `require': /gia/dev/helpers/subnav_helper.rb:36: syntax error, unexpected ',', expecting ')' (SyntaxError)
        create_subnav_link ("Campus Overview", "Overview"), 
                                              ^
/dev/helpers/subnav_helper.rb:36: syntax error, unexpected ')', expecting keyword_end
        create_subnav_link ("Campus Overview", "Overview"), 
                                                          ^
/dev/helpers/subnav_helper.rb:37: syntax error, unexpected ',', expecting ')'
        create_subnav_link ("About", "About"),  
                                             ^
/dev/helpers/subnav_helper.rb:37: syntax error, unexpected ')', expecting keyword_end
        create_subnav_link ("About", "About"),  
                                                      ^
/dev/helpers/subnav_helper.rb:39: syntax error, unexpected tLPAREN_ARG, expecting keyword_do or '{' or '('
        create_subnav_link ("Career Services" "...
                            ^
/dev/helpers/subnav_helper.rb:39: syntax error, unexpected ',', expecting keyword_end
...er Services" "CareerServices"),       
...                               ^
/dev/helpers/subnav_helper.rb:41: syntax error, unexpected tLPAREN_ARG, expecting keyword_do or '{' or '('
        create_subnav_link ("Financial Assistance & Schol...
                            ^
/dev/helpers/subnav_helper.rb:41: syntax error, unexpected ',', expecting keyword_end
...rships" "FinancialAssistance"),         
...                               ^
/dev/helpers/subnav_helper.rb:43: syntax error, unexpected ')', expecting keyword_end

Solution

  • def create_subnav (*links)
      subnav = "\
        <nav class='nav-sub'>\
          <div class='nav-wrap'>\
            <ul class='nav-list'>"
    
            links.each do |attr,val|
              subnav += " attr: " + attr;
              subnav += " val: " + val;
              # subnav += args["attr"] + "\n"
            end 
    
            subnav += "\
            </ul>\
          </div>\
        </nav>"
        return subnav
    end
    
    def create_campus_subnav
      subnav = create_subnav(
                              create_subnav_link("Campus Overview", "Overview"),
                              create_subnav_link("About", "About"),
                              create_subnav_link("Admissions"),
                              create_subnav_link("Career Services" "CareerServices"),
                              create_subnav_link("Schedules, Tuition & Fees" "Tuition"),
                              create_subnav_link("Financial Assistance & Scholarships" "FinancialAssistance"),
                              create_subnav_link("Faculty")
                             )
                             puts subnav
    end
    

    This will do it for you, but I think I would prefer the first entry to the argument list to be on the same line.