Class: Charty::Linspace

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/charty/linspace.rb

Instance Method Summary collapse

Constructor Details

#initialize(range, num_step) ⇒ Linspace

Returns a new instance of Linspace.



5
6
7
8
# File 'lib/charty/linspace.rb', line 5

def initialize(range, num_step)
  @range = range
  @num_step = num_step
end

Instance Method Details

#each(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/charty/linspace.rb', line 10

def each(&block)
  if @num_step == 1
    block.call(@range.begin)
  else
    step = (@range.end - @range.begin).to_r / (@num_step - 1)
    (@num_step - 1).times do |i|
      block.call(@range.begin + i * step)
    end

    unless @range.exclude_end?
      block.call(@range.end)
    end
  end
end