Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a dash "-" in a Ruby symbol?

I'm using jquery-mobile with Ruby On Rails.

I want to create a button link which implies to have data-role="button" appear in the generated HTML.

I've tried:

<%= link_to "Play", game_play_path, :data-role => "button" %> 

But then, I get an error

undefined local variable or method `role' for #<#<Class:0x007fdc25668ee8>:0x007fdc25658610> 

Is there a way to escape the dash using the :xxx notation or should I just use the "xxx" notation?

(I agree it's a cosmetic question, but I want my code to be consistent and don't like exceptions)

like image 958
Dirty Henry Avatar asked Dec 12 '11 22:12

Dirty Henry


People also ask

How do you make a new line in Ruby?

"\n" is newline, '\n\ is literally backslash and n.

How do symbols work in Ruby?

Ruby symbols are defined as “scalar value objects used as identifiers, mapping immutable strings to fixed internal values.” Essentially what this means is that symbols are immutable strings. In programming, an immutable object is something that cannot be changed.

How do I create a Symbol in Ruby?

Ruby symbols are created by placing a colon (:) before a word. You can think of it as an immutable string. A symbol is an instance of Symbol class, and for any given name of symbol there is only one Symbol object.


1 Answers

Use single quotes around the symbol name, with the colon prefix:

:'data-role' => 'button' 

And here is a nice reference on symbols:

http://www.troubleshooters.com/codecorn/ruby/symbols.htm#_What_do_symbols_look_like

After Ruby 1.9 you can also do

'data-role': 'button' 
like image 191
Andrew Kuklewicz Avatar answered Oct 07 '22 16:10

Andrew Kuklewicz