Enumeration with symbol name information


#1

Hello everybody…
I have a beginne’s question:

I’ve created class for my numeric constants, it looks like:

class MyClass

NAME1 = 0
NAME2 = 1
NAME3 = 2
#etc

def self.to_s(something)
case something
when 0
“NAME1”
when 1
“NAME2”
when 2
“NAME3”
#etc
end
end

end

What is more elegant way to do this? Basically, I want to convert the
numerical value to string representation (ideally via identifier name).

Thanks in advance,
Sasa


#2

On 7/20/07, Sasa removed_email_address@domain.invalid wrote:

NAME3 = 2
#etc
end
end

end

What is more elegant way to do this? Basically, I want to convert the
numerical value to string representation (ideally via identifier name).

In ruby we often use symbols in place of of enumerations. Any place
you’d
use NAME0, use :name0. For NAME1 use :name1, etc.

For example:
class MyClass
def something
case @enum_value
when :name0
puts “In name zero mode”
when :name1
puts “In name one mode”
end
end
end

:name0.to_s #=> “name0”

If, for whatever reason you don’t want to use symbols, a way I
occasionally
use to emulate enums is the following:

NAME0 = Object.new

def NAME0.to_s
“NAME0”
end

NAME1 = Object.new

def NAME1.to_s
“NAME1”
end

Another method that could be used to simplify your existing code is to
use a
hash:

NAME0 = 0
NAME1 = 1
NAME2 = 2

Names = { NAME0 => “NAME0”, NAME1 => “NAME1”, NAME2 => “NAME2” }

def self.to_s(something)
Names[something]
end

You can even wrap this one up (or the previous method with some
metaprogramming:)

def self.enum(*args)
const_set(“Names”, {})
args.each_with_index do |v, i|
Names[i] = v
const_set(v, i)
end
end

enum “NAME0”, “NAME1”, “NAME2”

Thanks in advance,


#3

On 2007-07-20 19:00:04 +0900 (Fri, Jul), Sasa wrote:

#etc
end
end

end

What is more elegant way to do this? Basically, I want to convert the
numerical value to string representation (ideally via identifier name).

Thanks in advance,
Sasa

If your numeric values always start from 0, then you may use an array:

class MyClass
NAMES = [ ‘NAME1’,‘NAME2’,‘NAME3’ ]

def self.to_s(something) # personally I would rather avoid redefining
class.to_s
NAMES[something.to_i] || default_value
end
end

If this is unacceptable, you may use a hash:

NAMES = { 1 => ‘NAME1’, 2 => ‘NAME2’, 5 => ‘NAME3’ }
NUMBERS = Hash[ *NAMES.map {|k,v| [v,k]}.flatten ] # may be useful
:slight_smile:

def self.to_s(something)
NAMES[ something ] || defaul_value
end

def self.to_i(something) # may be useful :slight_smile:
NUMBERS[ something ] || default_number
end