Class: MoreMath::StringNumeral
Defined Under Namespace
Modules: Functions
Constant Summary
NumberifyStringFunction::Functions
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
convert_alphabet, numberify_string, stringify_number
Constructor Details
#initialize(string, number, alphabet) ⇒ StringNumeral
Returns a new instance of StringNumeral.
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/more_math/string_numeral.rb', line 25
def initialize(string, number, alphabet)
@alphabet = NumberifyStringFunction.convert_alphabet(alphabet).freeze
if string
@string = string.to_s
string.each_char.each do |c|
@alphabet.include?(c) or raise ArgumentError,
"illegal character #{c.inspect} in #{@string.inspect} for alphabet #{@alphabet.inspect}"
end
elsif number
@number = number.to_i
end
end
|
Instance Attribute Details
Returns the value of attribute alphabet.
55
56
57
|
# File 'lib/more_math/string_numeral.rb', line 55
def alphabet
@alphabet
end
|
Class Method Details
.from(object, alphabet = 'a'..'z') ⇒ Object
.from_number(number, alphabet) ⇒ Object
21
22
23
|
# File 'lib/more_math/string_numeral.rb', line 21
def self.from_number(number, alphabet)
new nil, number, alphabet
end
|
.from_string(string, alphabet) ⇒ Object
17
18
19
|
# File 'lib/more_math/string_numeral.rb', line 17
def self.from_string(string, alphabet)
new string, nil, alphabet
end
|
Instance Method Details
77
78
79
|
# File 'lib/more_math/string_numeral.rb', line 77
def %(other)
self.class.from_number((number % naturalize(other)), @alphabet)
end
|
97
98
99
|
# File 'lib/more_math/string_numeral.rb', line 97
def &(other)
self.class.from_number(number & naturalize(other), @alphabet)
end
|
61
62
63
|
# File 'lib/more_math/string_numeral.rb', line 61
def *(other)
self.class.from_number(number * naturalize(other), @alphabet)
end
|
#**(other) ⇒ Object
81
82
83
|
# File 'lib/more_math/string_numeral.rb', line 81
def **(other)
self.class.from_number(number ** naturalize(other), @alphabet)
end
|
65
66
67
|
# File 'lib/more_math/string_numeral.rb', line 65
def +(other)
self.class.from_number(number + naturalize(other), @alphabet)
end
|
69
70
71
|
# File 'lib/more_math/string_numeral.rb', line 69
def -(other)
self.class.from_number(naturalize(number - other), @alphabet)
end
|
73
74
75
|
# File 'lib/more_math/string_numeral.rb', line 73
def /(other)
self.class.from_number((number / naturalize(other)), @alphabet)
end
|
#<<(other) ⇒ Object
85
86
87
|
# File 'lib/more_math/string_numeral.rb', line 85
def <<(other)
self.class.from_number(number << naturalize(other), @alphabet)
end
|
#>>(other) ⇒ Object
89
90
91
|
# File 'lib/more_math/string_numeral.rb', line 89
def >>(other)
self.class.from_number(number >> naturalize(other), @alphabet)
end
|
#[](other) ⇒ Object
105
106
107
|
# File 'lib/more_math/string_numeral.rb', line 105
def [](other)
self.class.from_number(number[other.to_i], @alphabet)
end
|
93
94
95
|
# File 'lib/more_math/string_numeral.rb', line 93
def ^(other)
self.class.from_number(number ^ naturalize(other), @alphabet)
end
|
#coerce(other) ⇒ Object
57
58
59
|
# File 'lib/more_math/string_numeral.rb', line 57
def coerce(other)
[ naturalize(other), number ]
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
129
130
131
132
133
134
135
|
# File 'lib/more_math/string_numeral.rb', line 129
def eql?(other)
if other.respond_to?(:to_int)
to_int == other.to_int
elsif other.respond_to?(:to_str)
to_str == other.to_str
end
end
|
139
140
141
|
# File 'lib/more_math/string_numeral.rb', line 139
def hash
number.hash
end
|
51
52
53
|
# File 'lib/more_math/string_numeral.rb', line 51
def inspect
"#<#{self.class}: #{string.inspect} #{number.inspect}>"
end
|
#number ⇒ Object
Also known as:
to_i, to_int
39
40
41
|
# File 'lib/more_math/string_numeral.rb', line 39
def number
@number ||= numberify_string(@string, @alphabet)
end
|
119
120
121
|
# File 'lib/more_math/string_numeral.rb', line 119
def pred
self.class.from_number(naturalize(number - 1), @alphabet)
end
|
123
124
125
126
127
|
# File 'lib/more_math/string_numeral.rb', line 123
def pred!
@number = naturalize(@number - 1)
@string = nil
self
end
|
#string ⇒ Object
Also known as:
to_s, to_str
45
46
47
|
# File 'lib/more_math/string_numeral.rb', line 45
def string
@string ||= stringify_number(@number, @alphabet).freeze
end
|
109
110
111
|
# File 'lib/more_math/string_numeral.rb', line 109
def succ
self.class.from_number(number + 1, @alphabet)
end
|
113
114
115
116
117
|
# File 'lib/more_math/string_numeral.rb', line 113
def succ!
@number += 1
@string = nil
self
end
|
101
102
103
|
# File 'lib/more_math/string_numeral.rb', line 101
def |(other)
self.class.from_number(number | naturalize(other), @alphabet)
end
|