Tbpgr Blog

Organization Development Engineer tbpgr(てぃーびー) のブログ

Ruby | Kernel | Array

概要

Kernel#Array

詳細

引数を配列にして返却します。
argのto_ary, to_aを順に実行。
どちらもない場合は1要素の配列として返却します。

サンプルコード
# encoding: utf-8
require 'tbpgr_utils'

class Person
  attr_reader :name, :age
  def initialize(name, age)
    @name, @age = name, age
  end
  def to_a
    [name, age]
  end
end

bulk_puts_eval binding, <<-EOS
Array([:hoge, :hige])
Array({hoge_key: :hoge_value, hige: :hige_value})
Array("string")
Array(1)
Array(Person.new("Tanaka", 25))
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
Array([:hoge, :hige]) # => [:hoge, :hige]
Array({hoge_key: :hoge_value, hige: :hige_value}) # => [[:hoge_key, :hoge_value], [:hige, :hige_value]]
Array("string") # => ["string"]
Array(1) # => [1]
Array(Person.new("Tanaka", 25)) # => ["Tanaka", 25]