Class: XlsFunction::FormatString::Evaluators::ValueStack

Inherits:
Object
  • Object
show all
Defined in:
lib/xls_function/format_string/evaluators/number_evaluator.rb

Overview

decomposes a number and takes from last.

Instance Method Summary collapse

Constructor Details

#initialize(source, dec_digit, format) ⇒ ValueStack

Returns a new instance of ValueStack.



186
187
188
189
190
191
192
193
194
195
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 186

def initialize(source, dec_digit, format)
  # reduce 1 because dec_digit include decimal point.
  # call to_d because round(0) in ruby 3.0 returns integer.
  value = dec_digit.positive? ? source.round(dec_digit - 1).to_d : source.round(0).to_d
  str = value.to_s('F')
  int, dec = str.split('.')

  @ints = format ? int_split_with_comma(int) : int.split('')
  @decs = dec_digit.positive? ? dec.split('') : []
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 225

def empty?
  @ints.empty? && @decs.empty?
end

#int_remain?Boolean

Returns:

  • (Boolean)


229
230
231
232
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 229

def int_remain?
  @ints.filter { |x| x != '-' }
       .any?
end

#int_split_with_comma(int_s) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 197

def int_split_with_comma(int_s)
  result = []
  count = 0
  arr = int_s.split('')
  length = arr.length
  length.times do |i|
    result.unshift(arr.pop)
    count += 1
    next if count < 3 || i == length - 1

    result.unshift(',')
    count = 0
  end
  result
end

#pop_intObject



213
214
215
216
217
218
219
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 213

def pop_int
  s = @ints.pop
  return s unless @ints.last == ','

  # return with comma if exists
  @ints.pop + s
end

#shift_decObject



221
222
223
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 221

def shift_dec
  @decs.shift
end

#to_sObject



234
235
236
237
238
239
# File 'lib/xls_function/format_string/evaluators/number_evaluator.rb', line 234

def to_s
  s = @ints.join
  return s if @decs.empty? || @decs == ['0']

  "#{s}.#{@decs.join}"
end