Class: MoreMath::StringNumeral

Inherits:
Object
  • Object
show all
Includes:
NumberifyStringFunction
Defined in:
lib/more_math/string_numeral.rb

Defined Under Namespace

Modules: Functions

Constant Summary

Constants included from NumberifyStringFunction

NumberifyStringFunction::Functions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NumberifyStringFunction

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

#alphabetObject (readonly)

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



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/more_math/string_numeral.rb', line 5

def self.from(object, alphabet = 'a'..'z')
  if Symbol === object
    StringNumeral.from_string(object.to_s, alphabet)
  elsif object.respond_to?(:to_str)
    StringNumeral.from_string(object.to_str, alphabet)
  elsif object.respond_to?(:to_int)
    StringNumeral.from_number(object.to_int, alphabet)
  else
    StringNumeral.from_string(object.to_s, alphabet)
  end
end

.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

#%(other) ⇒ Object



77
78
79
# File 'lib/more_math/string_numeral.rb', line 77

def %(other)
  self.class.from_number((number % naturalize(other)), @alphabet)
end

#&(other) ⇒ Object



97
98
99
# File 'lib/more_math/string_numeral.rb', line 97

def &(other)
  self.class.from_number(number & naturalize(other), @alphabet)
end

#*(other) ⇒ Object



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

#+(other) ⇒ Object



65
66
67
# File 'lib/more_math/string_numeral.rb', line 65

def +(other)
  self.class.from_number(number + naturalize(other), @alphabet)
end

#-(other) ⇒ Object



69
70
71
# File 'lib/more_math/string_numeral.rb', line 69

def -(other)
  self.class.from_number(naturalize(number - other), @alphabet)
end

#/(other) ⇒ Object



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

#^(other) ⇒ Object



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: ==

Returns:

  • (Boolean)


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

#hashObject



139
140
141
# File 'lib/more_math/string_numeral.rb', line 139

def hash
  number.hash
end

#inspectObject



51
52
53
# File 'lib/more_math/string_numeral.rb', line 51

def inspect
  "#<#{self.class}: #{string.inspect} #{number.inspect}>"
end

#numberObject 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

#predObject



119
120
121
# File 'lib/more_math/string_numeral.rb', line 119

def pred
  self.class.from_number(naturalize(number - 1), @alphabet)
end

#pred!Object



123
124
125
126
127
# File 'lib/more_math/string_numeral.rb', line 123

def pred!
  @number = naturalize(@number - 1)
  @string = nil
  self
end

#stringObject 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

#succObject



109
110
111
# File 'lib/more_math/string_numeral.rb', line 109

def succ
  self.class.from_number(number + 1, @alphabet)
end

#succ!Object



113
114
115
116
117
# File 'lib/more_math/string_numeral.rb', line 113

def succ!
  @number += 1
  @string = nil
  self
end

#|(other) ⇒ Object



101
102
103
# File 'lib/more_math/string_numeral.rb', line 101

def |(other)
  self.class.from_number(number | naturalize(other), @alphabet)
end