D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Struct
/
Filename :
new-c.ri
back
Copy
U:RDoc::AnyMethod[iI"new:ETI"Struct::new;TT:publico:RDoc::Markup::Document:@parts[/o:RDoc::Markup::Paragraph; [I"O<tt>Struct.new</tt> returns a new subclass of +Struct+. The new subclass:;To:RDoc::Markup::BlankLine o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"BMay be anonymous, or may have the name given by +class_name+.;To;;0; [o; ; [I"1May have members as given by +member_names+.;To;;0; [o; ; [I"MMay have initialization via ordinary arguments, or via keyword arguments;T@o; ; [I">The new subclass has its own method <tt>::new</tt>; thus:;T@o:RDoc::Markup::Verbatim; [I":Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo ;TI"Qf = Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1> ;T:@format0o; ; [I"<b>\Class Name</b>;T@o; ; [I"(With string argument +class_name+, ;TI"Sreturns a new subclass of +Struct+ named <tt>Struct::<em>class_name</em></tt>:;T@o;; [I":Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo ;TI"<Foo.name # => "Struct::Foo" ;TI"5Foo.superclass # => Struct ;T;0o; ; [I"+Without string argument +class_name+, ;TI"2returns a new anonymous subclass of +Struct+:;T@o;; [I"*Struct.new(:foo, :bar).name # => nil ;T;0o; ; [I"<b>Block</b>;T@o; ; [I"FWith a block given, the created subclass is yielded to the block:;T@o;; [I"GCustomer = Struct.new('Customer', :name, :address) do |new_class| ;TI", p "The new subclass is #{new_class}" ;TI" def greeting ;TI"' "Hello #{name} at #{address}" ;TI" end ;TI")end # => Struct::Customer ;TI"-dave = Customer.new('Dave', '123 Main') ;TI"Ndave # => #<struct Struct::Customer name="Dave", address="123 Main"> ;TI"1dave.greeting # => "Hello Dave at 123 Main" ;T;0o; ; [I"&Output, from <tt>Struct.new</tt>:;T@o;; [I","The new subclass is Struct::Customer" ;T;0o; ; [I"<b>Member Names</b>;T@o; ; [I"%Symbol arguments +member_names+ ;TI"0determines the members of the new subclass:;T@o;; [I"=Struct.new(:foo, :bar).members # => [:foo, :bar] ;TI"=Struct.new('Foo', :foo, :bar).members # => [:foo, :bar] ;T;0o; ; [I"KThe new subclass has instance methods corresponding to +member_names+:;T@o;; [ I")Foo = Struct.new('Foo', :foo, :bar) ;TI"AFoo.instance_methods(false) # => [:foo, :bar, :foo=, :bar=] ;TI"Mf = Foo.new # => #<struct Struct::Foo foo=nil, bar=nil> ;TI"*f.foo # => nil ;TI"(f.foo = 0 # => 0 ;TI"*f.bar # => nil ;TI"(f.bar = 1 # => 1 ;TI"If # => #<struct Struct::Foo foo=0, bar=1> ;T;0o; ; [I"<b>Singleton Methods</b>;T@o; ; [I"CA subclass returned by Struct.new has these singleton methods:;T@o;; ;;[o;;0; [o; ; [I"A\Method <tt>::new </tt> creates an instance of the subclass:;T@o;; [I"BFoo.new # => #<struct Struct::Foo foo=nil, bar=nil> ;TI"@Foo.new(0) # => #<struct Struct::Foo foo=0, bar=nil> ;TI">Foo.new(0, 1) # => #<struct Struct::Foo foo=0, bar=1> ;TI"BFoo.new(0, 1, 2) # Raises ArgumentError: struct size differs ;TI" ;TI".# Initialization with keyword arguments: ;TI"GFoo.new(foo: 0) # => #<struct Struct::Foo foo=0, bar=nil> ;TI"EFoo.new(foo: 0, bar: 1) # => #<struct Struct::Foo foo=0, bar=1> ;TI"%Foo.new(foo: 0, bar: 1, baz: 2) ;TI"3# Raises ArgumentError: unknown keywords: baz ;T;0o;;0; [o; ; [I"O\Method <tt>:inspect</tt> returns a string representation of the subclass:;T@o;; [I"Foo.inspect ;TI"# => "Struct::Foo" ;T;0o;;0; [o; ; [I"E\Method <tt>::members</tt> returns an array of the member names:;T@o;; [I"#Foo.members # => [:foo, :bar] ;T;0o; ; [I"<b>Keyword Argument</b>;T@o; ; [I"PBy default, the arguments for initializing an instance of the new subclass ;TI"2can be both positional and keyword arguments.;T@o; ; [I"OOptional keyword argument <tt>keyword_init:</tt> allows to force only one ;TI"&type of arguments to be accepted:;T@o;; [I"?KeywordsOnly = Struct.new(:foo, :bar, keyword_init: true) ;TI"&KeywordsOnly.new(bar: 1, foo: 0) ;TI".# => #<struct KeywordsOnly foo=0, bar=1> ;TI"KeywordsOnly.new(0, 1) ;TI"7# Raises ArgumentError: wrong number of arguments ;TI" ;TI"BPositionalOnly = Struct.new(:foo, :bar, keyword_init: false) ;TI"PositionalOnly.new(0, 1) ;TI"0# => #<struct PositionalOnly foo=0, bar=1> ;TI"(PositionalOnly.new(bar: 1, foo: 0) ;TI"C# => #<struct PositionalOnly foo={:foo=>1, :bar=>2}, bar=nil> ;TI"M# Note that no error is raised, but arguments treated as one hash value ;TI" ;TI"+# Same as not providing keyword_init: ;TI"5Any = Struct.new(:foo, :bar, keyword_init: nil) ;TI"Any.new(foo: 1, bar: 2) ;TI"%# => #<struct Any foo=1, bar=2> ;TI"Any.new(1, 2) ;TI"$# => #<struct Any foo=1, bar=2>;T;0: @fileI" struct.c;T:0@omit_headings_from_table_of_contents_below0I";Struct.new(*member_names, keyword_init: nil){|Struct_subclass| ... } -> Struct_subclass Struct.new(class_name, *member_names, keyword_init: nil){|Struct_subclass| ... } -> Struct_subclass Struct_subclass.new(*member_names) -> Struct_subclass_instance Struct_subclass.new(**member_names) -> Struct_subclass_instance ;T0[ I"(*args, p2 = {});T@�FI"Struct;TcRDoc::NormalClass00