Skip to content

Instantly share code, notes, and snippets.

@mutle
Created February 5, 2011 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutle/812859 to your computer and use it in GitHub Desktop.
Save mutle/812859 to your computer and use it in GitHub Desktop.
➜ cat test_cattr_accessor.rb
require 'rubygems'
require 'active_support/all'
class A
cattr_accessor :attribute
end
class B
cattr_accessor :attribute
end
A.attribute = "PASS"
B.attribute = "FAIL"
puts A.attribute
➜ rvm use system
Now using system ruby.
➜ ruby test_cattr_accessor.rb
PASS
➜ ~/tmp/rubinius/bin/rbx -v
rubinius 1.2.1dev (1.8.7 a0ef1c8f 2010-12-21 JI) [x86_64-apple-darwin10.6.0]
➜ ~/tmp/rubinius/bin/rbx test_cattr_accessor.rb
FAIL
➜ rvm use rbx-1.2.0
Using /Users/mutle/.rvm/gems/rbx-1.2.0-20101221
➜ ruby test_cattr_accessor.rb
FAIL
➜ rvm use rbx-1.1.1
Using /Users/mutle/.rvm/gems/rbx-1.1.1-20101116
➜ ruby test_cattr_accessor.rb
FAIL
➜ rvm use rbx-1.1.0
Using /Users/mutle/.rvm/gems/rbx-1.1.0-20100923
➜ ruby test_cattr_accessor.rb
PASS
➜ rvm use rbx-1.0.1
Using /Users/mutle/.rvm/gems/rbx-1.0.1-20100603
➜ ruby test_cattr_accessor.rb
PASS
require 'rubygems'
module ClassAttribute
class << self
def included(mod)
mod.class_eval <<-EOS
unless defined? @@attribute
@@attribute = nil
end
def self.attribute
@@attribute
end
def self.attribute=(obj)
@@attribute = obj
end
EOS
end
end
end
class A
include ClassAttribute
# --- This code works ---
# unless defined? @@attribute
# @@attribute = nil
# end
# def self.attribute
# @@attribute
# end
# def self.attribute=(obj)
# @@attribute = obj
# end
end
class B
include ClassAttribute
# --- This code works ---
# unless defined? @@attribute
# @@attribute = nil
# end
# def self.attribute
# @@attribute
# end
# def self.attribute=(obj)
# @@attribute = obj
# end
end
A.attribute = "PASS"
B.attribute = "FAIL"
puts A.attribute
# - FAIL in rbx, PASS in 1.8.7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment