Class: Adhearsion::VoIP::DSL::NumericalString
- Defined in:
- lib/adhearsion/voip/dsl/numerical_string.rb
Overview
In Ruby, a number with a leading zero such as 023.45 or 023 does not evaluate as expected. In the case of the float 023.45, you get a syntax error. In the case of 023 the number is parsed as being in octal form and the number 19 is returned.
In Adhearsion, various strings that are entirely numeric might start with zero and that zero should be preserved when converting that string of numbers into a number. The numerical string retains the zero while still allowing it to be compared to other numbers.
[[I think this leading zero thing is only part of the reason that NumericalString exists. I’m currently writing tests for this leading zero stuff so I thought I’d dump some of my assumptions about it here in the documentation.]]
Direct Known Subclasses
Instance Attribute Summary collapse
-
#__real_num ⇒ Object
readonly
Returns the value of attribute __real_num.
-
#__real_string ⇒ Object
readonly
Returns the value of attribute __real_string.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(str) ⇒ NumericalString
constructor
A new instance of NumericalString.
- #method_missing(name, *args, &block) ⇒ Object
- #respond_to?(m) ⇒ Boolean
Constructor Details
#initialize(str) ⇒ NumericalString
Returns a new instance of NumericalString.
26 27 28 29 |
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 26 def initialize(str) @__real_string = str.to_s @__real_num = str.to_i if @__real_string =~ /^\d+$/ end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
31 32 33 |
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 31 def method_missing(name, *args, &block) @__real_string.__send__ name, *args, &block end |
Instance Attribute Details
#__real_num ⇒ Object (readonly)
Returns the value of attribute __real_num.
24 25 26 |
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 24 def __real_num @__real_num end |
#__real_string ⇒ Object (readonly)
Returns the value of attribute __real_string.
24 25 26 |
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 24 def __real_string @__real_string end |
Class Method Details
.starts_with_leading_zero?(string) ⇒ Boolean
17 18 19 |
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 17 def starts_with_leading_zero?(string) string.to_s[/^0\d+/] end |
Instance Method Details
#respond_to?(m) ⇒ Boolean
35 36 37 |
# File 'lib/adhearsion/voip/dsl/numerical_string.rb', line 35 def respond_to?(m) @__real_string.respond_to?(m) || m == :__real_num || m == :__real_string end |