rails
(3,818 views)

A cute little underscore thingy

I found myself frequently doing something like "table_#{id}", so i added a little string method that lets me say 'table'._(id)

Rails provides a dom_id helper which lets you generate a DOM ID string for an object, for example, if @foo is class Foo with id=123, then @foo.dom_id #=> 'foo_123' .  But I don't always want to key off an object, and sometimes I need several ID's for different elements all associated with one object. I started getting annoyed writing things like,

 

<div id=<%= "#{name}_#{@foo.id}" %> >

 

So i wrote a little convenience method for strings:

 

class String  
  def _(id)
    self+"_#{id}"
  end
end     

 

 And now can say,

 

<div id=<%= name._ @foo.id %> >

 

Maybe its the same difference, but I find it convenient.

 

 

Comments

New Comment

markdown formatting permitted