Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | Array options extraction

概要

Array options extraction

詳細

Array options extraction について

Array#options extraction

配列引数の最後のHashを抽出するArray#extract_options!メソッド
Arrayの最後のHashを返却する。最後の要素がHashでなければ空のHashを返却する。

サンプル

# encoding: utf-8
require 'active_support/core_ext/array/extract_options'
require 'tbpgr_utils'

I18n.enforce_available_locales = true
bulk_puts_eval binding, <<-EOS
[1,2,3, hoge: 'hoge', hige: 'hige'].extract_options!
[1,2,3, {hoge: 'hoge', hige: 'hige'}].extract_options!
[1,2,3].extract_options!
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

[1,2,3, hoge: 'hoge', hige: 'hige'].extract_options! # => {:hoge=>"hoge", :hige=>"hige"}
[1,2,3, {hoge: 'hoge', hige: 'hige'}].extract_options! # => {:hoge=>"hoge", :hige=>"hige"}
[1,2,3].extract_options! # => {}