Class: Livetext::Variables
Instance Attribute Summary collapse
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Instance Method Summary collapse
- #[](var) ⇒ Object
- #[]=(var, value) ⇒ Object
- #get(var) ⇒ Object
-
#initialize(hash = {}) ⇒ Variables
constructor
A new instance of Variables.
- #inspect ⇒ Object
- #set(var, value) ⇒ Object
- #setvars(pairs) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Variables
Returns a new instance of Variables.
11 12 13 14 15 16 17 18 19 |
# File 'lib/livetext/more.rb', line 11 def initialize(hash = {}) @vars = {} hash.each_pair do |k, v| sym = k.to_sym str = k.to_s @vars[sym] = v @vars[str] = v end end |
Instance Attribute Details
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
9 10 11 |
# File 'lib/livetext/more.rb', line 9 def vars @vars end |
Instance Method Details
#[](var) ⇒ Object
30 31 32 |
# File 'lib/livetext/more.rb', line 30 def [](var) @vars[var.to_sym] end |
#[]=(var, value) ⇒ Object
34 35 36 |
# File 'lib/livetext/more.rb', line 34 def []=(var, value) @vars[var.to_sym] = value end |
#get(var) ⇒ Object
38 39 40 |
# File 'lib/livetext/more.rb', line 38 def get(var) @vars[var.to_sym] || "[#{var} is undefined]" end |
#inspect ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/livetext/more.rb', line 21 def inspect syms = @vars.keys.select {|x| x.is_a? Symbol } out = "\nVariables:" syms.each do |sym| out << " #{sym}: #{@vars[sym].inspect}\n" end out end |
#set(var, value) ⇒ Object
42 43 44 |
# File 'lib/livetext/more.rb', line 42 def set(var, value) @vars[var.to_sym] = value.to_s end |
#setvars(pairs) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/livetext/more.rb', line 46 def setvars(pairs) pairs = pairs.to_a if pairs.is_a?(Hash) pairs.each do |var, value| api.setvar(var, value) end end |
#to_a ⇒ Object
53 54 55 |
# File 'lib/livetext/more.rb', line 53 def to_a @vars.to_a end |