Created
September 15, 2009 22:37
-
-
Save anonymous/187725 to your computer and use it in GitHub Desktop.
This file contains 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
class SignalVariable | |
attr_reader :value | |
def add(val) | |
#we switch from a raw string to an array if this is the second time we've added | |
if @value | |
#add the new value to the array | |
#it looks strange but it doesn't matter how many times you have added an item | |
#it's easier then an if x then y else z concept to me | |
@value = [@value, val].flatten | |
else | |
@value = val | |
end | |
end | |
end | |
class Signal | |
def initialize() | |
#setup the hash and have it create the variable object for every new item | |
@variables = Hash.new{ |h,k| h[k] = SignalVariable.new } | |
end | |
def add_variable(name, value) | |
@variables[name].add(value) | |
end | |
def method_missing(name, *args) | |
#this will allow a call to Signal.xxx to return xxx automatically | |
#no need to define every variable | |
return @variables[name].value if @variables.key?(name) | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment