<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Enumerations and Ruby</title>
	<link>http://www.rubyfleebie.com/enumerations-and-ruby/</link>
	<description>Because programming should be fun</description>
	<pubDate>Fri, 12 Mar 2010 19:34:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>

	<item>
		<title>By: Ben</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-138</link>
		<author>Ben</author>
		<pubDate>Wed, 18 Apr 2007 00:03:57 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-138</guid>
					<description>It seems to me that symbols have made enumerations redundant. Why not just use :blue, :red and :yellow, period?</description>
		<content:encoded><![CDATA[<p>It seems to me that symbols have made enumerations redundant. Why not just use :blue, :red and :yellow, period?</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Badaud</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-140</link>
		<author>Badaud</author>
		<pubDate>Wed, 18 Apr 2007 11:09:23 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-140</guid>
					<description>[code]We don’t create instances of class Color because we don’t need to.[/code]
Actually, you have to, otherwise you're just defining constants and not Enums.

And 
Color.BLUE could be equal to Chess.KING

(Enums are not just about constant values, they're about type safety)</description>
		<content:encoded><![CDATA[<p>[code]We don’t create instances of class Color because we don’t need to.[/code]<br />
Actually, you have to, otherwise you&#8217;re just defining constants and not Enums.</p>
<p>And<br />
Color.BLUE could be equal to Chess.KING</p>
<p>(Enums are not just about constant values, they&#8217;re about type safety)</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-141</link>
		<author>Frank</author>
		<pubDate>Wed, 18 Apr 2007 11:23:21 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-141</guid>
					<description>@Ben,

In this regard, symbols could be seen as "top level" enums. However, the advantage of putting related constants inside a class is that it adds semantics to your application. The constants can appear logically grouped instead of floating in the air on their own. Of course, I'm not saying that these custom enums should replace symbols. I just find it useful to group things that are related together.

@Badaud,

As you can see in my exemple, I don't create any instance of class Color. I only work with the object Color, which is an instance of class Class. Also, you'll note that i don't write Color.BLUE but Color::BLUE, which triggers the const_missing method. It would be equivalent of doing : Color.const_missing(:BLUE) but there won't be any point to access it that way! Finally, I don't quite understand your claim about type safety. This was a practical example illustrating how to create an enum-like class in ruby. If for some reason Color::BLUE and Chess::KING would be equal... then so what? Those are integers values after all. The purpose (in this example at least) was to use an enum as a set of constants logically grouped together. (Like if "Color" was a namespace)</description>
		<content:encoded><![CDATA[<p>@Ben,</p>
<p>In this regard, symbols could be seen as &#8220;top level&#8221; enums. However, the advantage of putting related constants inside a class is that it adds semantics to your application. The constants can appear logically grouped instead of floating in the air on their own. Of course, I&#8217;m not saying that these custom enums should replace symbols. I just find it useful to group things that are related together.</p>
<p>@Badaud,</p>
<p>As you can see in my exemple, I don&#8217;t create any instance of class Color. I only work with the object Color, which is an instance of class Class. Also, you&#8217;ll note that i don&#8217;t write Color.BLUE but Color::BLUE, which triggers the const_missing method. It would be equivalent of doing : Color.const_missing(:BLUE) but there won&#8217;t be any point to access it that way! Finally, I don&#8217;t quite understand your claim about type safety. This was a practical example illustrating how to create an enum-like class in ruby. If for some reason Color::BLUE and Chess::KING would be equal&#8230; then so what? Those are integers values after all. The purpose (in this example at least) was to use an enum as a set of constants logically grouped together. (Like if &#8220;Color&#8221; was a namespace)</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Rik Hemsley</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-142</link>
		<author>Rik Hemsley</author>
		<pubDate>Wed, 18 Apr 2007 11:24:44 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-142</guid>
					<description>If you want to iterate through every item 'as if it was a collection', why not use a collection?

Colors = { :blue =&#62; 1, :red =&#62; 2, :yellow =&#62; 5 }</description>
		<content:encoded><![CDATA[<p>If you want to iterate through every item &#8216;as if it was a collection&#8217;, why not use a collection?</p>
<p>Colors = { :blue =&gt; 1, :red =&gt; 2, :yellow =&gt; 5 }</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-143</link>
		<author>Frank</author>
		<pubDate>Wed, 18 Apr 2007 11:50:47 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-143</guid>
					<description>@Rik, 

With your code, you would have to create an instance of Hash class everytime you would want to use the collection. If "Colors" is a local variable, it would goes out of scope pretty quick. If it is an instance variable, you would still have one hash for every instances of the class that contains it. What is the other option?  To make your hash global? hmm.. bad idea :)

If you use a class like i did and define your methods as class methods, you work with a single instance everytime : The instance of class "Class" called Color.</description>
		<content:encoded><![CDATA[<p>@Rik, </p>
<p>With your code, you would have to create an instance of Hash class everytime you would want to use the collection. If &#8220;Colors&#8221; is a local variable, it would goes out of scope pretty quick. If it is an instance variable, you would still have one hash for every instances of the class that contains it. What is the other option?  To make your hash global? hmm.. bad idea <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you use a class like i did and define your methods as class methods, you work with a single instance everytime : The instance of class &#8220;Class&#8221; called Color.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Rik Hemsley</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-144</link>
		<author>Rik Hemsley</author>
		<pubDate>Wed, 18 Apr 2007 11:55:52 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-144</guid>
					<description>Of course the Hash should be created in the scope where it'll be used. That will possibly be in global scope. Why is that a bad idea? I presume you think it's ok for your class to be in global scope?

If you're worried about Colors being a variable, freeze it.</description>
		<content:encoded><![CDATA[<p>Of course the Hash should be created in the scope where it&#8217;ll be used. That will possibly be in global scope. Why is that a bad idea? I presume you think it&#8217;s ok for your class to be in global scope?</p>
<p>If you&#8217;re worried about Colors being a variable, freeze it.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-145</link>
		<author>Frank</author>
		<pubDate>Wed, 18 Apr 2007 12:08:23 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-145</guid>
					<description>An enum must not be considered like a variable, but like a type. 

Plus, if you write : Colors = { :blue =&gt; 1, :red =&gt; 2, :yellow =&gt; 5 }, you're violating a ruby coding convention which says that you must only use lowercase for naming a local / instance variable.

So, to be nice you would have to write : colors = { :blue =&gt; 1, :red =&gt; 2, :yellow =&gt; 5 } and thus indicating clearly to the outside world that colors is a "single instance of something" instead of just "being something" on it's own. That's why I prefer using an enum-like class over using an instance of Hash class.</description>
		<content:encoded><![CDATA[<p>An enum must not be considered like a variable, but like a type. </p>
<p>Plus, if you write : Colors = { :blue => 1, :red => 2, :yellow => 5 }, you&#8217;re violating a ruby coding convention which says that you must only use lowercase for naming a local / instance variable.</p>
<p>So, to be nice you would have to write : colors = { :blue => 1, :red => 2, :yellow => 5 } and thus indicating clearly to the outside world that colors is a &#8220;single instance of something&#8221; instead of just &#8220;being something&#8221; on it&#8217;s own. That&#8217;s why I prefer using an enum-like class over using an instance of Hash class.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Rik Hemsley</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-146</link>
		<author>Rik Hemsley</author>
		<pubDate>Wed, 18 Apr 2007 12:21:11 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-146</guid>
					<description>1. I agree that an enum shouldn't be considered 'like a variable' - but like a type? In Ruby, an object's type is how it quacks. If I can get the value associated with the word 'red' using Colors[:red], the object is of an appropriate type.

The convention is that variables start with a lower case letter, local or not. Enumerations are (usually) treated as constant, which is why I named Colors with an initial capital.</description>
		<content:encoded><![CDATA[<p>1. I agree that an enum shouldn&#8217;t be considered &#8216;like a variable&#8217; - but like a type? In Ruby, an object&#8217;s type is how it quacks. If I can get the value associated with the word &#8216;red&#8217; using Colors[:red], the object is of an appropriate type.</p>
<p>The convention is that variables start with a lower case letter, local or not. Enumerations are (usually) treated as constant, which is why I named Colors with an initial capital.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Mr eel</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-150</link>
		<author>Mr eel</author>
		<pubDate>Wed, 18 Apr 2007 22:53:13 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-150</guid>
					<description>" a ruby coding convention which says that you must only use lowercase for naming a local / instance variable"

Actually not a convention at all but a feature of the language. In this case the name 'Colors' becomes a constant because it begins with a capital. 

This is an interesting use of const_missing. You've given me a bit to think about here.</description>
		<content:encoded><![CDATA[<p>&#8221; a ruby coding convention which says that you must only use lowercase for naming a local / instance variable&#8221;</p>
<p>Actually not a convention at all but a feature of the language. In this case the name &#8216;Colors&#8217; becomes a constant because it begins with a capital. </p>
<p>This is an interesting use of const_missing. You&#8217;ve given me a bit to think about here.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-154</link>
		<author>Frank</author>
		<pubDate>Thu, 19 Apr 2007 09:50:51 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-154</guid>
					<description>You're right. My bad on this one.</description>
		<content:encoded><![CDATA[<p>You&#8217;re right. My bad on this one.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Grant Hutchins</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-208</link>
		<author>Grant Hutchins</author>
		<pubDate>Wed, 16 May 2007 20:35:58 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-208</guid>
					<description>First off, you should use a Module, not a Class, since you're not going to be instantiating objects.

Second, I would suggest using a constant hash instead of @hash if it isn't going to change.

So I'd suggest:

module Color
  COLORS = [:blue, :red:, :yellow]
end

or instead of defining each at all, just do Color::COLORS.each and it's built right in.</description>
		<content:encoded><![CDATA[<p>First off, you should use a Module, not a Class, since you&#8217;re not going to be instantiating objects.</p>
<p>Second, I would suggest using a constant hash instead of @hash if it isn&#8217;t going to change.</p>
<p>So I&#8217;d suggest:</p>
<p>module Color<br />
  COLORS = [:blue, :red:, :yellow]<br />
end</p>
<p>or instead of defining each at all, just do Color::COLORS.each and it&#8217;s built right in.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Design Decisions &#60; Ardekantur</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-211</link>
		<author>Design Decisions &#60; Ardekantur</author>
		<pubDate>Thu, 17 May 2007 05:14:40 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-211</guid>
					<description>[...] to play, if we&#8217;re waiting to validate their e-mail, et cetera. I was about to create a pseudo-enumeration to deal with this, when I stopped. How much complexity needs to go into this? I could use a simple [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] to play, if we&#8217;re waiting to validate their e-mail, et cetera. I was about to create a pseudo-enumeration to deal with this, when I stopped. How much complexity needs to go into this? I could use a simple [&#8230;]</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: siroj</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-298</link>
		<author>siroj</author>
		<pubDate>Thu, 12 Jul 2007 09:58:40 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-298</guid>
					<description>Frank, I think what @Badaud means is enums in C# (I don't really know about enums in Java, maybe it's the same, I only used it once a year ago, already forgot). 

In C#, if you declare a method parameter to be some kind of enum, then you have to pass EXACTLY the MEMBER of that enum. So in C#, enums is more than just some constants grouped in one place, but it also has type information. If you pass different kind of enum than the one that method expected, then the compiler will happily report it as error to you.

But I think is quite impossible to do it in Ruby (actually, I haven't thinking about it :D), because usually we don't pay much attention to type information of an object. Event worse, actually Ruby has different concept about type (type-is-all-about-what-an-object-can-do vs type-is-all-about-object-class-hierarchy)</description>
		<content:encoded><![CDATA[<p>Frank, I think what @Badaud means is enums in C# (I don&#8217;t really know about enums in Java, maybe it&#8217;s the same, I only used it once a year ago, already forgot). </p>
<p>In C#, if you declare a method parameter to be some kind of enum, then you have to pass EXACTLY the MEMBER of that enum. So in C#, enums is more than just some constants grouped in one place, but it also has type information. If you pass different kind of enum than the one that method expected, then the compiler will happily report it as error to you.</p>
<p>But I think is quite impossible to do it in Ruby (actually, I haven&#8217;t thinking about it :D), because usually we don&#8217;t pay much attention to type information of an object. Event worse, actually Ruby has different concept about type (type-is-all-about-what-an-object-can-do vs type-is-all-about-object-class-hierarchy)</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-300</link>
		<author>Frank</author>
		<pubDate>Thu, 12 Jul 2007 21:44:11 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-300</guid>
					<description>Yes, you are right siroj. The enum described in this article was not an enum in the pure sense of the word... as it isn't a "type" per se.

Most of the time, I only need the kind of enum described in this article... or I don't need an enum at all. I just like to "group names under a common namespace" for code readability. I rarely need  something fancier.

Thanks for your comment</description>
		<content:encoded><![CDATA[<p>Yes, you are right siroj. The enum described in this article was not an enum in the pure sense of the word&#8230; as it isn&#8217;t a &#8220;type&#8221; per se.</p>
<p>Most of the time, I only need the kind of enum described in this article&#8230; or I don&#8217;t need an enum at all. I just like to &#8220;group names under a common namespace&#8221; for code readability. I rarely need  something fancier.</p>
<p>Thanks for your comment</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Alex Egg</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-343</link>
		<author>Alex Egg</author>
		<pubDate>Sat, 25 Aug 2007 06:10:54 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-343</guid>
					<description>Consider this enum of the style you describe:

  class Status
      Complete=0
      Queued=1
      Ready=2
      Processing=3
      Waiting=4
  end

now say I have an instance of this status:

status=Status::Waiting

How could I get this as a string that says "Waiting"? Even thought status is 4. Maybe create a class method in Status called to_string or something?</description>
		<content:encoded><![CDATA[<p>Consider this enum of the style you describe:</p>
<p>  class Status<br />
      Complete=0<br />
      Queued=1<br />
      Ready=2<br />
      Processing=3<br />
      Waiting=4<br />
  end</p>
<p>now say I have an instance of this status:</p>
<p>status=Status::Waiting</p>
<p>How could I get this as a string that says &#8220;Waiting&#8221;? Even thought status is 4. Maybe create a class method in Status called to_string or something?</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-344</link>
		<author>Frank</author>
		<pubDate>Mon, 27 Aug 2007 16:11:16 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-344</guid>
					<description>Alex,

That's a good question. Instead of using a Fixnum as the value, you could use a Hash :

class Status
Complete={:value =&gt; 0, :string =&gt; "complete"}
Queued={:value =&gt; 1, :string =&gt; "queued"}
Ready={:value =&gt; 2, :string =&gt; "ready"}
Processing={:value =&gt; 3, :string =&gt; "processing"}
Waiting={:value =&gt; 4, :string =&gt; "waiting"}
end

status = Status::Waiting
status[:value] # output : 4
status[:string] # output : waiting

There might be better ways to achieve what you want, but I think this solution would work fine.</description>
		<content:encoded><![CDATA[<p>Alex,</p>
<p>That&#8217;s a good question. Instead of using a Fixnum as the value, you could use a Hash :</p>
<p>class Status<br />
Complete={:value => 0, :string => &#8220;complete&#8221;}<br />
Queued={:value => 1, :string => &#8220;queued&#8221;}<br />
Ready={:value => 2, :string => &#8220;ready&#8221;}<br />
Processing={:value => 3, :string => &#8220;processing&#8221;}<br />
Waiting={:value => 4, :string => &#8220;waiting&#8221;}<br />
end</p>
<p>status = Status::Waiting<br />
status[:value] # output : 4<br />
status[:string] # output : waiting</p>
<p>There might be better ways to achieve what you want, but I think this solution would work fine.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Allex</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-502</link>
		<author>Allex</author>
		<pubDate>Thu, 03 Jan 2008 20:03:59 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-502</guid>
					<description>Look at &lt;a href="http://elhumidor.blogspot.com/2007/11/renum-ruby-enum-gem.html" rel="nofollow"&gt;Renum&lt;/A&gt; - "a Ruby enum gem".</description>
		<content:encoded><![CDATA[<p>Look at <a href="http://elhumidor.blogspot.com/2007/11/renum-ruby-enum-gem.html" rel="nofollow">Renum</a> - &#8220;a Ruby enum gem&#8221;.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Karlin Fox</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-846</link>
		<author>Karlin Fox</author>
		<pubDate>Wed, 24 Jun 2009 19:16:09 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-846</guid>
					<description>You may also be interested in Enumeration: http://github.com/karlin/enumeration/tree/master</description>
		<content:encoded><![CDATA[<p>You may also be interested in Enumeration: <a href="http://github.com/karlin/enumeration/tree/master" rel="nofollow">http://github.com/karlin/enumeration/tree/master</a></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-847</link>
		<author>Frank</author>
		<pubDate>Thu, 25 Jun 2009 00:47:19 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-847</guid>
					<description>Hey Thanks for the links!</description>
		<content:encoded><![CDATA[<p>Hey Thanks for the links!</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: jeff</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-855</link>
		<author>jeff</author>
		<pubDate>Wed, 15 Jul 2009 01:06:32 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-855</guid>
					<description>Might also take a look at enumerated_attribute: http://github.com/jeffp/enumerated_attribute/tree/master.  It works like...

enum_attr :gear, %w(reverse ^neutral first second third)</description>
		<content:encoded><![CDATA[<p>Might also take a look at enumerated_attribute: <a href="http://github.com/jeffp/enumerated_attribute/tree/master." rel="nofollow">http://github.com/jeffp/enumerated_attribute/tree/master.</a>  It works like&#8230;</p>
<p>enum_attr :gear, %w(reverse ^neutral first second third)</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Brent</title>
		<link>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-859</link>
		<author>Brent</author>
		<pubDate>Wed, 19 Aug 2009 20:46:51 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/enumerations-and-ruby/#comment-859</guid>
					<description>very helpful (even comments), thanks!!</description>
		<content:encoded><![CDATA[<p>very helpful (even comments), thanks!!</p>
]]></content:encoded>
				</item>
</channel>
</rss>
