Module: KoremuArrayFunctions
Overview
KoremuArrayFunctions
The following functions are either available through the KoremuArray class, or can be included in other classes. For example, maybe the most natural use would be by including them in the Array class. You can include them by typing this in your initialization:
class Array; include KoremuArrayFunctions; end
This way, any arbitrary array can be treated as a KoremuArray:
puts [10,50,120,3].to_ks # DUMISTABO
puts [10,50,120,3].to_ki # 21806083
If a KoremuArray includes any invalid element, it will be ignored and skipped - The elements should all be integers between 0 and 128:
irb(main):004:0> KoremuArray.new([1,2,10,100,1000]).to_ks.to_ka
Invalid Koremutake element '1000'
=> [1, 2, 10, 100]
If you choose not to extend Array, you can use KoremuArray as a regular class:
ks = KoremuArray.new([39, 67, 52, 78, 37]) # KOREMUTAKE
Notice the array should be given as the first element in the initialization. This is to keep consistency with the built-in Array behaviour.
Constant Summary
Constants included from Koremutake
Koremutake::Phonemes, Koremutake::Vowels
Instance Method Summary collapse
-
#to_ki ⇒ Object
Returns the KoremuFixnum representation of the KoremuArray.
-
#to_ks ⇒ Object
Returns the KoremuString representation of the KoremuArray.
-
#valid? ⇒ Boolean
Check if the given KoremuArray is valid, returning true or false.
Instance Method Details
#to_ki ⇒ Object
Returns the KoremuFixnum representation of the KoremuArray
257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/koremu.rb', line 257 def to_ki res = 0 self.each do |num| unless valid_elem?(num) warn "Invalid Koremutake element '#{num}'" next end res *= 128 res += num end KoremuFixnum.new(res) end |
#to_ks ⇒ Object
Returns the KoremuString representation of the KoremuArray
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/koremu.rb', line 237 def to_ks rel = nums_to_phonemes ks = KoremuString.new('') # Drop any stream of leading zeroes. However, be consistent: We # are representing values. Return a 0 if we have no value yet self.shift while self[0] == 0 self << 0 if self.empty? self.each do |num| unless valid_elem?(num) warn "Invalid Koremutake element '#{num}'" next end ks << rel[num] end ks end |
#valid? ⇒ Boolean
Check if the given KoremuArray is valid, returning true or false. If it is not valid, it can still be used - Non-recognized elements will just be dropped, so:
puts KoremuArray.new([39, 67, 52, 200, 78, 37]).to_ki # 10610353957
puts KoremuArray.new([39, 67, 52, 78, 37]).to_ki # 10610353957
puts KoremuArray.new([39, 67, 52, 200, 78, 37]).to_ks # KOREMUTAKE
puts KoremuArray.new([39, 67, 52, 78, 37]).to_ks # KOREMUTAKE
In both cases, the ‘200’ is dropped (and a warning issued, of course).
Remember that Koremutake is a way to represent numbers - Thus, although zeros are valid, they would be collapsed at the beginning
-
i.e. [0, 0, 0, 0, 1] would become [1]. So, as there is
potentially information loss, 0 is not valid as a first element.
228 229 230 231 232 233 234 |
# File 'lib/koremu.rb', line 228 def valid? self.each do |elem| return false unless valid_elem?(elem) end return false if self[0] == 0 true end |