かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Ruby]ActiveSupportのcattr_accessor

HAL99さんの[Rails]仮想カラムの作り方を見てcattr_accessorの存在を知った!!
その後、ActiveRecordのコードを読み始めたら、cattr_accessorのコードがちらほら出てきたので気になって調べて見た。

ソースコードを、探して見たらactive_support/core_ext/class/attribute_accessors.rbにあった!
どうやらこれは、クラス変数を定義するためのものらしい。
クラス変数をインスタンス変数ちっくにアクセスできるものも定義してくれる。
早速実験!!

cattr_sample.rb
require 'rubygems'
require 'active_support'

class Hoge
  # クラス変数fooを定義
  cattr_accessor :foo
end

# クラス変数だけどインスタンス変数のようにアクセスできる
h = Hoge.new
h.foo = "hogehoge"
puts h.foo    # => hogehoge

# 別のインスタンスでも同じように設定できる
h2 = Hoge.new
puts h2.foo   # => hogehoge
h2.foo = "mogemoge"
puts h2.foo    # => mogemoge

# もちろんクラス変数としてもアクセスできる
puts Hoge.foo  # => mogemoge

これをやって思った。
仮想カラムの作り方に書いてある使い方は、複数のインスタンスがあるとガクガクブルブルなことに…。

投稿日時 : 2007年11月29日 23:05

Feedback

# Cheap Canada Goose 2012/10/19 15:49 http://www.supercoatsale.com

I really like your writing style, great info, appreciate it for posting :D. "I will show you fear in a handful of dust." by T. S. Eliot.

# バーバリー 財布 メンズ 2012/11/06 21:43 http://burberry.suppa.jp/

今日は よろしくお願いしますね^^すごいですね^^

# Supra Skytop III 2012/12/07 19:26 http://supratkstore.webs.com/

Some genuinely excellent blog posts on this web site , thanks for contribution.

# men moncler jackets 2012/12/08 13:21 http://supermonclercoats1.webs.com/

Great ?I should certainly pronounce, impressed with your website. I had no trouble navigating through all tabs as well as related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Excellent task.

# longchamp bags black 2012/12/14 23:08 http://www.longchampbagoutlet.info/category/longch

I am sure I can visit this place ever again soon.

# エルメス店舗 2012/12/15 16:12 http://www.hermespairs.info/category/エルメスバーキン

I am sure I will visit this approach place once again soon.

# burberry uk 2012/12/16 4:58 http://www.burberryuksale.info/category/burberry-u

I would like to take the closer have a look at some of this memorabilia!

# sac michael kors nouveau 2012/12/17 8:29 http://www.sacmichaelkors.net/michael-kors-nouveau

i recommend you upon your great content and articles and wonderful topic alternatives.

# burberry solde 2012/12/17 21:35 http://www.sacburberryecharpe.fr/category/burberry

I truly like em. It's dislike you dress yourself in these out walking around town most people.

# le pliage longchamps tote 2012/12/17 21:44 http://www.sacslongchamp2012.info/longchamps-darsh

I have not looked within Sennheisers along with am short of new tote.

# michael kors sac solde 2012/12/18 20:56 http://michael-kors-canada.webnode.fr/news-/

I realize everybody should hate in it, but I don't even think they appear so lousy.

# cheap burberry outlet 2012/12/18 21:23 http://www.burberryoutlet2012.info/category/burber

keep up the good job!

# sacs Burberry 2012/12/18 22:40 http://sacburberrysoldesfr.webnode.fr

I have never looked into Sennheisers as well as am wanting new tote.

# longchamp le pliage 2012/12/21 7:43 http://longchampukbags.webeden.co.uk

I in reality like em. It's not wish you dress in these out walking around town many people.

# OcxrhPYKSUQXdDZWV 2015/05/04 11:22 horny

LBavAQ http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

タイトル
名前
Url
コメント