Created
November 20, 2012 11:40
-
-
Save t0d0r/4117441 to your computer and use it in GitHub Desktop.
sample ruby class using Stuct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # TODO: finish this | |
| class Colors | |
| attr_accessor :color | |
| ColorAttr = Struct.new(:name, :hex) | |
| { | |
| :silver => ColorAttr.new("silver", "ff0000"), | |
| :blue => ColorAttr.new("blue", "ff0000"), | |
| :black => ColorAttr.new("black", "ff0000"), | |
| :white => ColorAttr.new("white", "ff0000"), | |
| :red => ColorAttr.new("red", "ff0000"), | |
| :green => ColorAttr.new("green", "00ff00"), | |
| :other => ColorAttr.new("other", "00ff00"), | |
| } | |
| def initialize | |
| @color = { | |
| :silver => ColorAttr.new("silver", "ff0000"), | |
| :blue => ColorAttr.new("blue", "ff0000"), | |
| :black => ColorAttr.new("black", "ff0000"), | |
| :white => ColorAttr.new("white", "ff0000"), | |
| :red => ColorAttr.new("red", "ff0000"), | |
| :green => ColorAttr.new("green", "00ff00"), | |
| :other => ColorAttr.new("other", "00ff00"), | |
| } | |
| end | |
| def list | |
| color.keys | |
| end | |
| def random_pick | |
| k = color.keys | |
| k[rand(k.size)] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment