Class: NTable::LabeledAxis

Inherits:
Object
  • Object
show all
Defined in:
lib/ntable/axis.rb

Overview

An axis in which the labels are explicitly provided as strings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*labels_) ⇒ LabeledAxis

Create a LabeledAxis given an array of the label strings. Symbols may also be provided, but will be converted to strings.



125
126
127
128
129
130
131
# File 'lib/ntable/axis.rb', line 125

def initialize(*labels_)
  labels_ = labels_.flatten
  @a = labels_.map{ |label_| label_.to_s }
  @h = {}
  @a.each_with_index{ |n_, i_| @h[n_] = i_ }
  @size = labels_.size
end

Instance Attribute Details

#sizeObject (readonly)

See EmptyAxis#size



153
154
155
# File 'lib/ntable/axis.rb', line 153

def size
  @size
end

Instance Method Details

#eql?(obj_) ⇒ Boolean Also known as: ==

See EmptyAxis#eql?

Returns:

  • (Boolean)


135
136
137
# File 'lib/ntable/axis.rb', line 135

def eql?(obj_)
  obj_.is_a?(LabeledAxis) && @a.eql?(obj_.instance_variable_get(:@a))
end

#from_json_object(json_obj_) ⇒ Object

See EmptyAxis#from_json_object



173
174
175
# File 'lib/ntable/axis.rb', line 173

def from_json_object(json_obj_)
  initialize(json_obj_['labels'] || [])
end

#hashObject

See EmptyAxis#hash



141
142
143
# File 'lib/ntable/axis.rb', line 141

def hash
  @a.hash
end

#index(label_) ⇒ Object

See EmptyAxis#index



157
158
159
# File 'lib/ntable/axis.rb', line 157

def index(label_)
  @h[label_.to_s]
end

#inspectObject Also known as: to_s

See EmptyAxis#inspect



146
147
148
# File 'lib/ntable/axis.rb', line 146

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} #{@a.inspect}>"
end

#label(index_) ⇒ Object

See EmptyAxis#label



162
163
164
# File 'lib/ntable/axis.rb', line 162

def label(index_)
  @a[index_]
end

#to_json_object(json_obj_) ⇒ Object

See EmptyAxis#to_json_object



168
169
170
# File 'lib/ntable/axis.rb', line 168

def to_json_object(json_obj_)
  json_obj_['labels'] = @a
end