D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Data
/
Filename :
define-c.ri
back
Copy
U:RDoc::AnyMethod[iI"define:ETI"Data::define;TT:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"Defines a new \Data class.;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I"+measure = Data.define(:amount, :unit) ;TI"%#=> #<Class:0x00007f70c6868498> ;TI"measure.new(1, 'km') ;TI"%#=> #<data amount=1, unit="km"> ;TI" ;TI";# It you store the new class in the constant, it will ;TI"8# affect #inspect and will be more natural to use: ;TI"+Measure = Data.define(:amount, :unit) ;TI"#=> Measure ;TI"Measure.new(1, 'km') ;TI"-#=> #<data Measure amount=1, unit="km"> ;T:@format0o; ; [I"ONote that member-less \Data is acceptable and might be a useful technique ;TI"8for defining several homogeneous data classes, like;T@o;; [ I"class HTTPFetcher ;TI"% Response = Data.define(:body) ;TI" NotFound = Data.define ;TI" # ... implementation ;TI" end ;T; 0o; ; [I"PNow, different kinds of responses from +HTTPFetcher+ would have consistent ;TI"representation:;T@o;; [I"3#<data HTTPFetcher::Response body="<html..."> ;TI"##<data HTTPFetcher::NotFound> ;T; 0o; ; [I"3And are convenient to use in pattern matching:;T@o;; [I"case fetcher.get(url) ;TI"$in HTTPFetcher::Response(body) ;TI" # process body variable ;TI"in HTTPFetcher::NotFound ;TI" # handle not found case ;TI"end;T; 0: @fileI" struct.c;T:0@omit_headings_from_table_of_contents_below0I"define(*symbols) -> class ;T0[ I"(*args);T@:FI" Data;TcRDoc::NormalClass00