Inverse of id2name?

If I have a variable set to :test, I can convert that to “test” by calling
id2name (aka to_s) on it.

irb(main):006:0> x = :test
:test
irb(main):007:0> x.id2name
"test"

If I have a variable set to “test”, how do I convert that to :test? I
suppose I could use eval()…

irb(main):008:0> eval “:#{x}”
:test

but is there a more efficient way to do this?

Philip Mak mfraser@seas.gwu.edu writes:

If I have a variable set to “test”, how do I convert that to :test?

a.intern

Cheers

Dave

I was looking for the exact same thing last week – and being too timid to
ask the list did not find it – and went with eval. Hence two notes:

  • ri Fixnum.id2name refers to String.intern, but ri Symbol.id2name does
    not. In the next update to ri’s db, maybe this could be added?

  • alias String.intern to String.name2id and String.to_symbol (or
    String.to_sym) for moron’s like me. :wink:

– Nikodemus

···

On Sun, 22 Sep 2002, Dave Thomas wrote:

If I have a variable set to “test”, how do I convert that to :test?

a.intern

I may have beed deaf and blind, but Dave really should, while standing on
the soapbox for RDoc, also mention simple_markup explicitly. It is a very
nice tool, especially with eruby. Maybe not so much for regular CGI-work,
but for offline generation of simple pages it’s perfect!

I especially like the fact that it does not add any , , or

stuff to the html it generates. This make the inclusion of bits generated by it to html nice and easy.

I recently discovered it and, and did something like this (it’s a bit ugly
’cos I was in a hurry so I snipped the guts):

require 'markup/simple_markup’
require ‘markup/simple_markup/to_html’

class Rhtml

@@parser = SM::SimpleMarkup.new
@@handler = SM::ToHtml.new

def self.doctype
end

def self.head (title, css)
end

def self.page (body, title, css)
end

def self.simple_page (file, title, css)
body = self.parse_simple (file)
self.page ("#{body}", title, css)
end

def self.parse_simple( file )
txt = File.open(file) { |f| f.read }
@@parser.convert(txt, @@handler)
end

def self.parse_rhtml( file )
eruby #{file}
end

end

So the I could do:

<% require ‘rhtml’ %>
…html…
<%=Rhtml.parse_simple(“foo.txt”)%>
…html…

or

<% require ‘rhtml’; Rhtml.simple_page(“foo.txt”, “Foo”, “foo.css”) %>

– Nikodemus

Nikodemus Siivola tsiivola@cc.hut.fi writes:

  • ri Fixnum.id2name refers to String.intern, but ri Symbol.id2name does
    not. In the next update to ri’s db, maybe this could be added?

I’ve updated the book’s errata. Good catch.

Dave