i have a basic ruby class:
class LogEntry
end
and what i would like to do is be able to define a hash with a few values like so:
EntryType = { :error => 0, :warning => 1, :info => 2 }
so that i can access the values like this (or something similar):
LogEntry.EntryType[:error]
is this even possible in Ruby? am i going about this the right way?
You can do this:
class LogEntry
EntryType = { :error => 0, :warning => 1, :info => 2 }
end
But you want to reference it as
LogEntry::EntryType[:error]
Alternatively you could make a class method:
class LogEntry
def self.types
{ :error => 0, :warning => 1, :info => 2 }
end
end
# And a simple test
LogEntry.types[:error].should be_an_instance_of(Hash)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With