Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created October 10, 2008 01:26
Show Gist options
  • Save ELLIOTTCABLE/15957 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/15957 to your computer and use it in GitHub Desktop.
a fork of a fork!
require 'attr_splat'
class Class
class self.singleton
# We don't do this by default, just to be safe. It's perfectly acceptable,
# as attr_*'s API is fully backwards compatible.
#
# Now every example below, attr_splat could safely be replaced with
# attr_accessor
alias_method :attr_accessor, :attr_splat
end
end
# Reader-only examples
class Something
# Ruby Core
attr_reader :something
# attr_*
attr_splat :something, :writer => false
# Ruby Core
attr_reader :something, :something_else
# attr_*
attr_splat :something, :something_else, :writer => false
end
# Writer-only examples
class Something
# Ruby Core
attr_writer :something
# attr_*
attr_splat :something, :reader => false
# Ruby Core
attr_writer :something, :something_else
# attr_*
attr_splat :something, :something_else, :reader => false
end
# Accessor examples
class Something
# Ruby Core
attr_accessor :something
# attr_*
attr_splat :something
# Ruby Core
attr_accessor :something, :something_else
# attr_*
attr_splat :something, :something_else
end
# Initializer examples
class Something
# Ruby Core
attr_accessor :something
def initialize
@something = nil
end
# attr_*
attr_splat :something, :initialize => true
# Ruby Core
attr_accessor :something, :something_else
def initialize
@something = nil
@something_else = nil
end
# attr_*
attr_splat :something, :something_else, :initialize => true
end
# Default value examples
class Something
# Ruby Core
attr_accessor :something
def something
@something ||= 123
end
# attr_*
attr_splat :something, :default => 123
# Ruby Core
attr_accessor :something, :something_else
def something
@something ||= 123
end
def something_else
@something_else ||= 123
end
# attr_*
attr_splat :something, :something_else, :default => 123
end
# Initialized default value examples
class Something
# Ruby Core
attr_accessor :something
def initialize
@something = 123
end
# attr_*
attr_splat :something, :initialize => true, :default => 123
# Ruby Core
attr_accessor :something, :something_else
def initialize
@something = 123
@something_else = 123
end
# attr_*
attr_splat :something, :something_else, :initialize => true, :default => 123
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment