Module: DaruLite::Vector::Convertible
- Included in:
- DaruLite::Vector
- Defined in:
- lib/daru_lite/vector/convertible.rb
Instance Method Summary collapse
-
#to_a ⇒ Object
Return an array.
-
#to_df ⇒ DaruLite::DataFrame
The vector as a single-vector dataframe.
-
#to_h ⇒ Object
Convert to hash (explicit).
-
#to_html(threshold = 30) ⇒ Object
Convert to html for iruby.
- #to_html_tbody(threshold = 30) ⇒ Object
- #to_html_thead ⇒ Object
-
#to_json ⇒ Object
Convert the hash from to_h to json.
-
#to_matrix(axis = :horizontal) ⇒ Object
Convert Vector to a horizontal or vertical Ruby Matrix.
- #to_s ⇒ Object
Instance Method Details
#to_a ⇒ Object
Return an array
31 32 33 |
# File 'lib/daru_lite/vector/convertible.rb', line 31 def to_a @data.to_a end |
#to_df ⇒ DaruLite::DataFrame
Returns the vector as a single-vector dataframe.
5 6 7 |
# File 'lib/daru_lite/vector/convertible.rb', line 5 def to_df DaruLite::DataFrame.new({ @name => @data }, name: @name, index: @index) end |
#to_h ⇒ Object
Convert to hash (explicit). Hash keys are indexes and values are the correspoding elements
26 27 28 |
# File 'lib/daru_lite/vector/convertible.rb', line 26 def to_h @index.to_h { |index| [index, self[index]] } end |
#to_html(threshold = 30) ⇒ Object
Convert to html for iruby
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/daru_lite/vector/convertible.rb', line 41 def to_html(threshold = 30) table_thead = to_html_thead table_tbody = to_html_tbody(threshold) path = if index.is_a?(MultiIndex) File.('../iruby/templates/vector_mi.html.erb', __dir__) else File.('../iruby/templates/vector.html.erb', __dir__) end ERB.new(File.read(path).strip).result(binding) end |
#to_html_tbody(threshold = 30) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/daru_lite/vector/convertible.rb', line 62 def to_html_tbody(threshold = 30) table_tbody_path = if index.is_a?(MultiIndex) File.('../iruby/templates/vector_mi_tbody.html.erb', __dir__) else File.('../iruby/templates/vector_tbody.html.erb', __dir__) end ERB.new(File.read(table_tbody_path).strip).result(binding) end |
#to_html_thead ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/daru_lite/vector/convertible.rb', line 52 def to_html_thead table_thead_path = if index.is_a?(MultiIndex) File.('../iruby/templates/vector_mi_thead.html.erb', __dir__) else File.('../iruby/templates/vector_thead.html.erb', __dir__) end ERB.new(File.read(table_thead_path).strip).result(binding) end |
#to_json ⇒ Object
Convert the hash from to_h to json
36 37 38 |
# File 'lib/daru_lite/vector/convertible.rb', line 36 def to_json(*) to_h.to_json end |
#to_matrix(axis = :horizontal) ⇒ Object
Convert Vector to a horizontal or vertical Ruby Matrix.
Arguments
-
axis- Specify whether you want a :horizontal or a :vertical matrix.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/daru_lite/vector/convertible.rb', line 14 def to_matrix(axis = :horizontal) case axis when :horizontal Matrix[to_a] when :vertical Matrix.columns([to_a]) else raise ArgumentError, "axis should be either :horizontal or :vertical, not #{axis}" end end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/daru_lite/vector/convertible.rb', line 72 def to_s "#<#{self.class}#{": #{@name}" if @name}(#{size})#{':category' if category?}>" end |