зеркало из https://github.com/github/ruby.git
* lib/ostruct.rb: Add OpenStruct#to_h [Feature #6276]
[ref #1400] [rubyspec:9e0250b2fc6f] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35342 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
1dbe0f0672
Коммит
db66739c7c
4
NEWS
4
NEWS
|
@ -74,6 +74,10 @@ with all sufficient information, see the ChangeLog file.
|
|||
* Net::IMAP.default_ssl_port
|
||||
* Net::IMAP.default_imaps_port
|
||||
|
||||
* ostruct
|
||||
* new methods:
|
||||
* OpenStruct#to_h converts the struct to a hash.
|
||||
|
||||
* pathname
|
||||
* extended method:
|
||||
* Pathname#find returns an enumerator if no block is given.
|
||||
|
|
|
@ -101,32 +101,27 @@ class OpenStruct
|
|||
end
|
||||
|
||||
#
|
||||
# Provides marshalling support for use by the Marshal library. Returning the
|
||||
# underlying Hash table that contains the functions defined as the keys and
|
||||
# the values assigned to them.
|
||||
# Converts the OpenStruct to a hash with keys representing
|
||||
# each attribute (as symbols) and their corresponding values
|
||||
# Example:
|
||||
#
|
||||
# require 'ostruct'
|
||||
# require 'ostruct'
|
||||
# data = OpenStruct.new("country" => "Australia", :population => 20_000_000)
|
||||
# data.to_h # => {:country => "Australia", :population => 20000000 }
|
||||
#
|
||||
# person = OpenStruct.new
|
||||
# person.name = 'John Smith'
|
||||
# person.age = 70
|
||||
def to_h
|
||||
@table.dup
|
||||
end
|
||||
|
||||
#
|
||||
# person.marshal_dump # => { :name => 'John Smith', :age => 70 }
|
||||
# Provides marshalling support for use by the Marshal library.
|
||||
#
|
||||
def marshal_dump
|
||||
@table
|
||||
end
|
||||
|
||||
#
|
||||
# Provides marshalling support for use by the Marshal library. Accepting
|
||||
# a Hash of keys and values which will be used to populate the internal table
|
||||
#
|
||||
# require 'ostruct'
|
||||
#
|
||||
# event = OpenStruct.new
|
||||
# hash = { 'time' => Time.now, 'title' => 'Birthday Party' }
|
||||
# event.marshal_load(hash)
|
||||
# event.title # => 'Birthday Party'
|
||||
# Provides marshalling support for use by the Marshal library.
|
||||
#
|
||||
def marshal_load(x)
|
||||
@table = x
|
||||
|
|
Загрузка…
Ссылка в новой задаче