* lib/ostruct.rb: documented
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5785 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
464fbf7b4f
commit
0425463a3d
@ -1,3 +1,7 @@
|
|||||||
|
Thu Feb 19 22:39:00 2004 Gavin Sinclair <gsinclair@soyabean.com.au>
|
||||||
|
|
||||||
|
* lib/ostruct.rb: documented
|
||||||
|
|
||||||
Thu Feb 19 22:39:04 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
|
Thu Feb 19 22:39:04 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
|
||||||
|
|
||||||
* test/rinda/test_rinda.rb: DRb.start_service only once in testsuites.
|
* test/rinda/test_rinda.rb: DRb.start_service only once in testsuites.
|
||||||
|
@ -1,14 +1,48 @@
|
|||||||
# ostruct.rb - Python Style Object
|
|
||||||
# just assign to create field
|
|
||||||
#
|
#
|
||||||
# s = OpenStruct.new
|
# = ostruct.rb: OpenStruct implementation
|
||||||
# s.foo = 25
|
#
|
||||||
# p s.foo
|
# Author:: Yukihiro Matsumoto
|
||||||
# s.bar = 2
|
# Documentation:: Gavin Sinclair
|
||||||
# p s.bar
|
#
|
||||||
# p s
|
# OpenStruct allows the creation of data objects with arbitrary attributes.
|
||||||
|
# See OpenStruct for an example.
|
||||||
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# OpenStruct allows you to create data objects and set arbitrary attributes.
|
||||||
|
# For example:
|
||||||
|
#
|
||||||
|
# require 'ostruct'
|
||||||
|
#
|
||||||
|
# record = OpenStruct.new
|
||||||
|
# record.name = "John Smith"
|
||||||
|
# record.age = 70
|
||||||
|
# record.pension = 300
|
||||||
|
#
|
||||||
|
# puts record.name # -> "John Smith"
|
||||||
|
# puts record.address # -> nil
|
||||||
|
#
|
||||||
|
# It is like a hash with a different way to access the data. In fact, it is
|
||||||
|
# implemented with a hash, and you can initialize it with one.
|
||||||
|
#
|
||||||
|
# hash = { "country" => "Australia", :population => 20_000_000 }
|
||||||
|
# data = OpenStruct.new(hash)
|
||||||
|
#
|
||||||
|
# p data # -> <OpenStruct country="Australia" population=20000000>
|
||||||
|
#
|
||||||
class OpenStruct
|
class OpenStruct
|
||||||
|
#
|
||||||
|
# Create a new OpenStruct object. The optional +hash+, if given, will
|
||||||
|
# generate attributes and values. For example.
|
||||||
|
#
|
||||||
|
# require 'ostruct'
|
||||||
|
# hash = { "country" => "Australia", :population => 20_000_000 }
|
||||||
|
# data = OpenStruct.new(hash)
|
||||||
|
#
|
||||||
|
# p data # -> <OpenStruct country="Australia" population=20000000>
|
||||||
|
#
|
||||||
|
# By default, the resulting OpenStruct object will have no attributes.
|
||||||
|
#
|
||||||
def initialize(hash=nil)
|
def initialize(hash=nil)
|
||||||
@table = {}
|
@table = {}
|
||||||
if hash
|
if hash
|
||||||
@ -18,7 +52,7 @@ class OpenStruct
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(mid, *args)
|
def method_missing(mid, *args) # :nodoc:
|
||||||
mname = mid.id2name
|
mname = mid.id2name
|
||||||
len = args.length
|
len = args.length
|
||||||
if mname =~ /=$/
|
if mname =~ /=$/
|
||||||
@ -37,10 +71,16 @@ class OpenStruct
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Remove the named field from the object.
|
||||||
|
#
|
||||||
def delete_field(name)
|
def delete_field(name)
|
||||||
@table.delete name.to_sym
|
@table.delete name.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Returns a string containing a detailed summary of the keys and values.
|
||||||
|
#
|
||||||
def inspect
|
def inspect
|
||||||
str = "<#{self.class}"
|
str = "<#{self.class}"
|
||||||
for k,v in @table
|
for k,v in @table
|
||||||
@ -49,9 +89,12 @@ class OpenStruct
|
|||||||
str << ">"
|
str << ">"
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :table
|
attr_reader :table # :nodoc:
|
||||||
protected :table
|
protected :table
|
||||||
|
|
||||||
|
#
|
||||||
|
# Compare this object and +other+ for equality.
|
||||||
|
#
|
||||||
def ==(other)
|
def ==(other)
|
||||||
return false unless(other.kind_of?(OpenStruct))
|
return false unless(other.kind_of?(OpenStruct))
|
||||||
return @table == other.table
|
return @table == other.table
|
||||||
|
Loading…
x
Reference in New Issue
Block a user