D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Data
/
Filename :
cdesc-Data.ri
back
Copy
U:RDoc::NormalClass[iI" Data:ET@I"Object;To:RDoc::Markup::Document:@parts[o;;[o:RDoc::Markup::Paragraph;[I"E\Class \Data provides a convenient way to define simple classes ;TI"for value-alike objects.;To:RDoc::Markup::BlankLine o; ;[I"#The simplest example of usage:;T@o:RDoc::Markup::Verbatim;[I"+Measure = Data.define(:amount, :unit) ;TI" ;TI"4# Positional arguments constructor is provided ;TI"'distance = Measure.new(100, 'km') ;TI"/#=> #<data Measure amount=100, unit="km"> ;TI" ;TI"1# Keyword arguments constructor is provided ;TI"2weight = Measure.new(amount: 50, unit: 'kg') ;TI".#=> #<data Measure amount=50, unit="kg"> ;TI" ;TI"0# Alternative form to construct an object: ;TI" speed = Measure[10, 'mPh'] ;TI"/#=> #<data Measure amount=10, unit="mPh"> ;TI" ;TI"*# Works with keyword arguments, too: ;TI".area = Measure[amount: 1.5, unit: 'm^2'] ;TI"0#=> #<data Measure amount=1.5, unit="m^2"> ;TI" ;TI"(# Argument accessors are provided: ;TI"distance.amount #=> 100 ;TI"distance.unit #=> "km" ;T:@format0o; ;[I"AConstructed object also has a reasonable definitions of #== ;TI"Koperator, #to_h hash conversion, and #deconstruct / #deconstruct_keys ;TI"$to be used in pattern matching.;T@o; ;[I"C::define method accepts an optional block and evaluates it in ;TI"Cthe context of the newly defined class. That allows to define ;TI"additional methods:;T@o;;[I".Measure = Data.define(:amount, :unit) do ;TI" def <=>(other) ;TI"E return unless other.is_a?(self.class) && other.unit == unit ;TI"! amount <=> other.amount ;TI" end ;TI" ;TI" include Comparable ;TI" end ;TI" ;TI"0Measure[3, 'm'] < Measure[5, 'm'] #=> true ;TI"(Measure[3, 'm'] < Measure[5, 'kg'] ;TI"A# comparison of Measure with Measure failed (ArgumentError) ;T;0o; ;[ I"BData provides no member writers, or enumerators: it is meant ;TI"@to be a storage for immutable atomic values. But note that ;TI"Lif some of data members is of a mutable class, Data does no additional ;TI"immutability enforcement:;T@o;;[I"+Event = Data.define(:time, :weekdays) ;TI"1event = Event.new('18:00', %w[Tue Wed Fri]) ;TI"D#=> #<data Event time="18:00", weekdays=["Tue", "Wed", "Fri"]> ;TI" ;TI"C# There is no #time= or #weekdays= accessors, but changes are ;TI"# still possible: ;TI"event.weekdays << 'Sat' ;TI"event ;TI"K#=> #<data Event time="18:00", weekdays=["Tue", "Wed", "Fri", "Sat"]> ;T;0o; ;[I"?See also Struct, which is a similar concept, but has more ;TI"Dcontainer-alike API, allowing to change contents of the object ;TI"and enumerate it.;T: @fileI" struct.c;T:0@omit_headings_from_table_of_contents_below0; 0;0[ [ [ [[I" class;T[[:public[[I"define;TI" struct.c;T[I"members;T@f[I"new;T@f[:protected[ [:private[ [I" instance;T[[;[[I"==;T@f[I"deconstruct;T@f[I"deconstruct_keys;T@f[I" eql?;T@f[I" hash;T@f[I"inspect;T@f[I"members;T@f[I" to_h;T@f[I" to_s;T@f[I" with;T@f[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ; 0;0[I"lib/pp.rb;T@Z@ZcRDoc::TopLevel