array_to_dataset.rb
author: KUMAGAI Hidetake
array_to_dataset メソッド
array_to_dataset ( array , options={} )
配列の配列、ハッシュのハッシュなどをRDB::ClientDataSetにして返す。
次のような6つのタイプに対応します。
array_of_array …… 配列の配列 array_of_hash …… ハッシュの配列 array_of_struct …… Structオブジェクトの配列 hash_of_array …… 配列のハッシュ hash_of_hash …… ハッシュのハッシュ hash_of_struct …… Structオブジェクトのハッシュ
行がハッシュの場合(hash_of_xx …… xxのハッシュ)は、キーでソートし、値の配列に変換して処理します。 array_of_struct = hash_of_struct.sort.collect{|k,v|v}
列が Struct の場合には Struct の項目名が、データセットの項目名のデフォルトになります。
require "array_to_dataset" ItemStruct = Struct.new(:seisuu,:mojiretu,:jissuu) array_of_struct =[ ItemStruct.new(1,"いち",1.1), ItemStruct.new(2,"にい",2.2), ItemStruct.new(3,"さん",3.3), ] puts "\n array_to_dataset( array_of_struct ).display" array_to_dataset( array_of_struct ).display #=> seisuu mojiretu jissuu #=> -------------- -------------- -------------- #=> 1 いち 1.1 #=> 2 にい 2.2 #=> 3 さん 3.3
puts "\n array_to_dataset( array_of_struct ).display with :label=[]" array_to_dataset( array_of_struct , :labels=>["整数","文字列","実数"]).display #=> #=> #=> array_to_dataset( array_of_struct ).display with :label=[] #=> 整数 文字列 実数 #=> -------------- -------------- -------------- #=> 1 いち 1.1 #=> 2 にい 2.2 #=> 3 さん 3.3
hash_of_xx の場合には array_to_dataset の中でハッシュを配列に変換して array_of_xx の形にします。 その後で、array_of_xx の各メソッドに振り分けます。
array_of_struct_to_dataset …… Structオブジェクトの配列 array_of_hash_to_dataset …… array_of_array_to_dataset ……
array_of_array 配列の配列
配列の配列では順番は変りません。レコードは配列の順番通りに並びます。 フィールド名は names オプションで指定しない限り "field_0" というような平凡な名前になります。
require "array_to_dataset" array_of_array = [ [ 101 , 103 , 102 ], [ 301 , 303 , 302 ], [ 201 , 203 , 1111 ], ] array_to_dataset(array_of_array).display #=> field_0 field_1 field_2 #=> -------------- -------------- -------------- #=> 101 103 102 #=> 301 303 302 #=> 201 203 1111
array_of_hash ハッシュの配列
配列の内部にハッシュが入っている場合にはハッシュのキーが項目名となり、項目名をソートした順番で並びます。項目の順番は keys オプションで指定できます。
require "array_to_dataset" array_of_hash = [ {1 => 101 , 3 => 103 , 2 => 102 }, {1 => 301 , 3 => 303 , 2 => 302 }, {1 => 201 , 3 => 203 , 2 => 2222 }, ] array_to_dataset(array_of_hash ).display #=> 1 2 3 #=> -------------- -------------- -------------- #=> 101 102 103 #=> 301 302 303 #=> 201 2222 203
hash_of_array 配列のハッシュ
外側が配列でなくハッシュの場合には、キーの順番でレコードがソートされます。キーはデータセットには含まれません。 それ以外の点は array_of_array と同じです。
require "array_to_dataset" hash_of_array = { 1 => [ 101 , 103 , 102 ], 3 => [ 301 , 303 , 302 ], 2 => [ 201 , 203 , 3333 ], } array_to_dataset( hash_of_array).display #=> field_0 field_1 field_2 #=> -------------- -------------- -------------- #=> 101 103 102 #=> 201 203 3333 #=> 301 303 302
hash_of_hash ハッシュのハッシュ
外側が配列でなくハッシュの場合には、キーの順番でレコードがソートされます。キーはデータセットには含まれません。 それ以外の点は array_of_hash と同じです。
require "array_to_dataset" hash_of_hash = { 1 => {1 => 101 , 3 => 103 , 2 => 102 }, 3 => {1 => 301 , 3 => 303 , 2 => 302 }, 2 => {1 => 201 , 3 => 203 , 2 => 4444 }, } array_to_dataset( hash_of_hash ).display #=> 1 2 3 #=> -------------- -------------- -------------- #=> 101 102 103 #=> 201 4444 203 #=> 301 302 303
オプション
:keys => Array
内部がハッシュの場合に、ハッシュのキーの配列を指定する。項目の順番を指定するために使う。
require "array_to_dataset" hash_of_hash = { 1 => {1 => 101 , 3 => 103 , 2 => 102 }, 3 => {1 => 301 , 3 => 303 , 2 => 302 }, 2 => {1 => 201 , 3 => 203 , 2 => 4444 }, } array_to_dataset( hash_of_hash , {:keys=>[3,2,1]}).display #=> 3 2 1 #=> -------------- -------------- -------------- #=> 103 102 101 #=> 203 4444 201 #=> 303 302 301
:labels => Array
表示ラベルの配列。
require "array_to_dataset" array_of_array = [ [ 101 , 103 , 102 ], [ 301 , 303 , 302 ], [ 201 , 203 , 1111 ], ] array_to_dataset(array_of_array,{:labels=>["壱","参","弐"]}).display #=> 壱 参 弐 #=> -------------- -------------- -------------- #=> 101 103 102 #=> 301 303 302 #=> 201 203 1111
:names => Array
データセットの項目名の配列。日本語の項目名は保証しない。あまり使うことはないだろう。 * :labels のほうが自由度が高い。 * 内部がハッシュの場合に、キーが項目名賭して不適当な場合に有用か?
:types => Array
項目の型の配列。指定しなければ自動的に適当な項目の型がセットされます。
:sizes => Array
各項目の最低の長さの配列を指定する。文字列以外では意味がないでしょう。 各項目の長さは、(1)データの内容を調べた最大のデータ長さ、(2) :sizes で指定した長さ、(3) :size_mini で指定した長さのうちもっとも大きいものになります。 デフォルトの長さは14バイト(:size_mini のデフォルトの値)しかありません。すべて空の状態であれば、14バイトになってしまいます。
:size_mini => Integer
文字列項目の最少の長さを指定する。
Keyword(s):
References:[$(apollo)/lib/array_to_dataset.rb]