0

I often have to generate SVG files, and I like doing so with Ruby's Nokogiri. The nice thing about Nokogiri is that it lets you create attributes passing a hash to their "functions", like so

doc.rect(:x => 0, :y => 0, :width => 100, :height => 100)

which is great. There are some attributes that have hyphens: in that case you can just take advantage of Ruby's awesomeness and do something like

doc.rect(:x => 0, :y => 0, :width => 100, :height => 100, :stroke => 'black', 'stroke-width' => 3)

and all is relatively well. Enter Ruby 2.0 and named parameters. I much prefer this syntax, it's a bit more concise and a bit more smalltalkesque, which I like. However, the only way to create hyphenated attributes now is to mix the two approaches, provided that you place the 'hash' after the named parameters (I assume it has to be this way, but I haven't checked). In any case, it's ugly.

Is there any way you wise people can conjure up to create hyphenated attributes using the named parameters syntax?

EDIT: To clarify, named parameters look like this:

doc.rect(x: 0, y: 0, width: 100, height: 100)
Morpheu5
  • 2,610
  • 6
  • 39
  • 72
  • how are you using the named parameters syntax? could you give an example of a method with named parameters as you intend to write it? – Uri Agassi Nov 19 '14 at 11:33
  • @uri-agassi I clarified by editing the question. – Morpheu5 Nov 19 '14 at 12:17
  • 1
    That's not [named parameters](http://stackoverflow.com/questions/15308163/named-parameters-in-ruby-2) (also known as [keyword arguments](http://brainspec.com/blog/2012/10/08/keyword-arguments-ruby-2-0/), it is the new(ish) [hash syntax](http://www.shanison.com/2013/05/19/ruby-1-9-2-hash-syntax/), introduced in ruby 1.9.2... – Uri Agassi Nov 19 '14 at 12:45
  • I see. The new(ish) syntax confused me a bit, but it all makes sense now. Thanks for clarifying. – Morpheu5 Nov 19 '14 at 15:55

2 Answers2

2

Use either

'stroke-width'.to_sym

or

:'stroke-width'

Both evaluate to symbol. Actually, since the named parameters simply derived syntax from new ruby2 hash notation, you still may mix both like:

params = { named: 'Param1', :'old-style' => 'Param2' } 

and, hence:

doc.rect x: 0, y: 0, :'stroke-width' => 3

It’s the syntax sugar only, inside it is a well-known old good hash. BTW, there is no way to omit hash-rockets for keys, containing \Ws.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • That's not what I asked, though. The question I asked is how to use named parameters that result in hyphenated XML attributes through Nokogiri. The second line I pasted works already, I suspect Ruby takes care of that. – Morpheu5 Nov 19 '14 at 11:54
  • `:'stroke-width'` might be a name of named parameter. It’s a symbol, while `'stroke-width'` is a string. Either I don’t get what’s your problem, or you did not realize the difference between strings and symbols. – Aleksei Matiushkin Nov 19 '14 at 11:58
  • A symbol is not a named parameter. It's been used like that for a while, but Ruby 2.0 has proper named parameters. See the edit to my original question. – Morpheu5 Nov 19 '14 at 12:16
  • 1
    Ah, sorry, got your point now. Since the named parameters simply derived syntax from new ruby2 hash notation, you still are free to mix both like: `params = { named: 'Param1', :'old-style' => 'Param2' }` and, hence, `doc.rect x: 0, y: 0, :'stroke-width' => 3`. It’s the syntax sugar only, inside it is a well-known old good hash. – Aleksei Matiushkin Nov 19 '14 at 12:50
  • I see (also thanks to Uri's comment above :) The whole thing is a bit confused, but makes sense with some thought. – Morpheu5 Nov 19 '14 at 15:56
0

This is not possible. Hyphens are not legal in an identifier.

Think about it: how would you know whether a-b means the identifier a-b or a minus b?

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653