Skip to content

Instantly share code, notes, and snippets.

Created September 15, 2009 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/187725 to your computer and use it in GitHub Desktop.
Save anonymous/187725 to your computer and use it in GitHub Desktop.
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