Repository URL to install this package:
|
Version:
2.2.2-1 ▾
|
U:RDoc::NormalClass[iI"OpenStruct:ET@I"Object;To:RDoc::Markup::Document:@parts[o;;[o:RDoc::Markup::Paragraph;[I"-OpenStruct serialization/deserialization;T:
@fileI"%ext/json/lib/json/add/ostruct.rb;T:0@omit_headings_from_table_of_contents_below0o;;[o; ;[ I"KAn OpenStruct is a data structure, similar to a Hash, that allows the ;TI"Pdefinition of arbitrary attributes with their accompanying values. This is ;TI"Qaccomplished by using Ruby's metaprogramming to define methods on the class ;TI"itself.;To:RDoc::Markup::BlankLine S:RDoc::Markup::Heading:
leveli: textI"Examples:;T@o:RDoc::Markup::Verbatim;[I"require 'ostruct'
;TI"
;TI"person = OpenStruct.new
;TI"#person.name = "John Smith"
;TI"person.age = 70
;TI"person.pension = 300
;TI"
;TI",puts person.name # -> "John Smith"
;TI""puts person.age # -> 70
;TI"#puts person.address # -> nil
;T:@format0o; ;[I"QAn OpenStruct employs a Hash internally to store the methods and values and ;TI"&can even be initialized with one:;T@o;;[I"Taustralia = OpenStruct.new(:country => "Australia", :population => 20_000_000)
;TI"Mp australia # -> <OpenStruct country="Australia" population=20000000>
;T;0o; ;[ I"THash keys with spaces or characters that would normally not be able to use for ;TI"Hmethod calls (e.g. ()[]*) will not be immediately available on the ;TI"TOpenStruct object as a method for retrieval or assignment, but can be still be ;TI",reached through the Object#send method.;T@o;;[I"?measurements = OpenStruct.new("length (in inches)" => 24)
;TI"6measurements.send("length (in inches)") # -> 24
;TI"
;TI"3data_point = OpenStruct.new(:queued? => true)
;TI"8data_point.queued? # -> true
;TI"'data_point.send("queued?=",false)
;TI"9data_point.queued? # -> false
;T;0o; ;[I"ORemoving the presence of a method requires the execution the delete_field ;TI"Nmethod as setting the property value to +nil+ will not remove the method.;T@o;;[
I"Jfirst_pet = OpenStruct.new(:name => 'Rowdy', :owner => 'John Smith')
;TI"first_pet.owner = nil
;TI"3second_pet = OpenStruct.new(:name => 'Rowdy')
;TI"
;TI"*first_pet == second_pet # -> false
;TI"
;TI"$first_pet.delete_field(:owner)
;TI")first_pet == second_pet # -> true
;T;0S;
;i;I"Implementation:;T@o; ;[I"RAn OpenStruct utilizes Ruby's method lookup structure to find and define the ;TI"Onecessary methods for properties. This is accomplished through the method ;TI"&method_missing and define_method.;T@o; ;[I"SThis should be a consideration if there is a concern about the performance of ;TI"Qthe objects that are created, as there is much more overhead in the setting ;TI">of these properties compared to using a Hash or a Struct.;T;
I"lib/ostruct.rb;T;0;
0;0[ [ [ [[I"
class;T[[:public[[I"json_create;FI"%ext/json/lib/json/add/ostruct.rb;T[I"new;TI"lib/ostruct.rb;T[:protected[ [:private[ [I"
instance;T[[;[[I"==;T@h[I"[];F@h[I"[]=;F@h[I"as_json;F@e[I"delete_field;F@h[I"each_pair;F@h[I" eql?;F@h[I" hash;F@h[I"initialize_copy;F@h[I"inspect;F@h[I"marshal_dump;F@h[I"marshal_load;F@h[I" to_h;F@h[I"to_json;F@e[I" to_s;T@h[;[[I"modifiable;F@h[I"new_ostruct_member;F@h[;[ [ [U:RDoc::Context::Section[i 0o;;[ ;
0;0[@@Y@YcRDoc::TopLevel