Tbpgr Blog

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

ActiveSupport | Module#alias_attribute

概要

Module#alias_attribute

詳細

Module#alias_attributeについて

Module#alias_attribute

Attributeにエイリアスを付与する。

サンプル

# encoding: utf-8
require 'active_support/core_ext/module/aliasing'
require 'tbpgr_utils'
require 'attributes_initializable'

class Hoge
  include AttributesInitializable
  attr_accessor_init :hoge, :hige

  alias_attribute :new_hoge, :hoge
  alias_attribute :new_hige, :hige
end

h = Hoge.new do |h|
  h.hoge = "hoge"
  h.hige = "hige"
end

bulk_puts_eval binding, <<EOS
h.new_hoge
h.new_hige
EOS

__END__
下記はTbpgrUtils gemの機能

AttributesInitializable,attr_accessor_init
bulk_puts_eval

詳しくは下記参照
https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

h.new_hoge # => "hoge"
h.new_hige # => "hige"