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


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
# File 'lib/charty/linspace.rb', line 10

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

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