Class: Mikon::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/mikon/core/dataframe.rb

Overview

Row class for internal use

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, arr, index) ⇒ Row

Returns a new instance of Row.



371
372
373
374
375
# File 'lib/mikon/core/dataframe.rb', line 371

def initialize(labels, arr, index)
  @labels = labels
  @arr = arr
  @index = index
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Examples:

row = Row.new([:a, :b, :c], [1, 2, 3], :example_row)
puts row.instance_eval { a * b * c} #-> 7


395
396
397
398
399
# File 'lib/mikon/core/dataframe.rb', line 395

def method_missing(name, *args)
  super unless args.length == 0
  pos = @labels.index(name)
  pos.nil? ? super : @arr[pos]
end

Instance Attribute Details

#arrObject (readonly)

Returns the value of attribute arr.



408
409
410
# File 'lib/mikon/core/dataframe.rb', line 408

def arr
  @arr
end

#indexObject (readonly)

Returns the value of attribute index.



408
409
410
# File 'lib/mikon/core/dataframe.rb', line 408

def index
  @index
end

#labelsObject (readonly)

Returns the value of attribute labels.



408
409
410
# File 'lib/mikon/core/dataframe.rb', line 408

def labels
  @labels
end

Instance Method Details

#[](name) ⇒ Object



377
378
379
380
# File 'lib/mikon/core/dataframe.rb', line 377

def [](name)
  pos = @labels.index(name)
  pos.nil? ? nil : @arr[pos]
end

#[]=(name, val) ⇒ Object



382
383
384
385
386
387
388
389
390
# File 'lib/mikon/core/dataframe.rb', line 382

def []=(name, val)
  pos = @labels.index(name)
  if pos.nil?
    @labels.push(name)
    @arr.push(val)
  else
    @arr[pos] = val
  end
end

#to_hashObject



401
402
403
404
405
406
# File 'lib/mikon/core/dataframe.rb', line 401

def to_hash
  @labels.each.with_index.reduce({}) do |memo, (label, i)|
    memo[label] = @arr[i]
    memo
  end
end