8000 Enhanced RDoc for Struct by BurdetteLamar · Pull Request #4890 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Enhanced RDoc for Struct #4890

New issue < 8000 button aria-label="Close dialog" data-close-dialog="" type="button" data-view-component="true" class="Link--muted btn-link position-absolute p-4 right-0">

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 107 additions & 63 deletions struct.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ rb_struct_s_members_m(VALUE klass)
*
* Customer = Struct.new(:name, :address, :zip)
* Customer.new.members # => [:name, :address, :zip]
*
* Related: #to_a.
*/

static VALUE
Expand Down Expand Up @@ -868,21 +870,24 @@ struct_enum_size(VALUE s, VALUE args, VALUE eobj)

/*
* call-seq:
* struct.each {|obj| block } -> struct
* struct.each -> enumerator
* each {|value| ... } -> self
* each -> enumerator
*
* Yields the value of each struct member in order. If no block is given an
* enumerator is returned.
* Calls the given block with the value of each member; returns +self+:
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.each {|x| puts(x) }
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.each {|value| p value }
*
* Output:
*
* "Joe Smith"
* "123 Maple, Anytown NC"
* 12345
*
* Produces:
* Returns an Enumerator if no block is given.
*
* Joe Smith
* 123 Maple, Anytown NC
* 12345
* Related: #each_pair.
*/

static VALUE
Expand All @@ -899,21 +904,25 @@ rb_struct_each(VALUE s)

/*
* call-seq:
* struct.each_pair {|sym, obj| block } -> struct
* struct.each_pair -> enumerator
* each_pair {|(name, value)| ... } -> self
* each_pair -> enumerator
*
* Yields the name and value of each struct member in order. If no block is
* given an enumerator is returned.
* Calls the given block with each member name/value pair; returns +self+:
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.each_pair {|name, value| puts("#{name} => #{value}") }
* Customer = Struct.new(:name, :address, :zip) # => Customer
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.each_pair {|(name, value)| p "#{name} => #{value}" }
*
* Output:
*
* "name => Joe Smith"
* "address => 123 Maple, Anytown NC"
* "zip => 12345"
*
* Returns an Enumerator if no block is given.
*
* Produces:
* Related: #each.
*
* name => Joe Smith
* address => 123 Maple, Anytown NC
* zip => 12345
*/

static VALUE
Expand Down Expand Up @@ -986,11 +995,17 @@ inspect_struct(VALUE s, VALUE dummy, int recur)
}

/*
* call-seq:
* struct.to_s -> string
* struct.inspect -> string
* call-seq:
* inspect -> string
*
* Returns a string representation of +self+:
*
* Customer = Struct.new(:name, :address, :zip) # => Customer
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
*
* Struct#to_s is an alias for Struct#inspect.
*
* Returns a description of this struct as a string.
*/

static VALUE
Expand All @@ -1001,14 +1016,17 @@ rb_struct_inspect(VALUE s)

/*
* call-seq:
* struct.to_a -> array
* struct.values -> array
* to_a -> array
*
* Returns the values for this struct as an Array.
* Returns the values in +self+ as an array:
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.to_a[1] #=> "123 Maple, Anytown NC"
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
*
* Struct#values and Struct#deconstruct are aliases for Struct#to_a.
*
* Related: #members.
*/

static VALUE
Expand All @@ -1019,19 +1037,25 @@ rb_struct_to_a(VALUE s)

/*
* call-seq:
* struct.to_h -> hash
* struct.to_h {|name, value| block } -> hash
* to_h -> hash
* to_h {|name, value| ... } -> hash
*
* Returns a Hash containing the names and values for the struct's members.
* Returns a hash containing the name and value for each member:
*
* If a block is given, the results of the block on each pair of the receiver
* will be used as pairs.
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* h = joe.to_h
* h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
*
* If a block is given, it is called with each name/value pair;
* the block should return a 2-element array whose elements will become
* a key/value pair in the returned hash:
*
* h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}
* h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}
*
* Raises ArgumentError if the block returns an inappropriate value.
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe.to_h[:address] #=> "123 Maple, Anytown NC"
* joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}[:ADDRESS]
* #=> "123 MAPLE, ANYTOWN NC"
*/

static VALUE
Expand Down Expand Up @@ -1154,19 +1178,28 @@ invalid_struct_pos(VALUE s, VALUE idx)

/*
* call-seq:
* struct[member] -> object
* struct[index] -> object
* struct[name] -> object
* struct[n] -> object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a single line with key instead of name and n. From a caller's perspective, the two lines are the same. Same change for []=

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two forms seem different to me.

More importantly, replacing name and n with key would force some clumsiness into the discussion:

  • +key+th or something perhaps worse.
  • Raises NameError if +key+ is not an integer or a member name.
  • Raises IndexError if +key+ is an integer that is out of range.

I'd prefer to leave stet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is just a preference on my part, I'm fine leaving as-is.

*
* Attribute Reference---Returns the value of the given struct +member+ or
* the member at the given +index+. Raises NameError if the +member+ does
* not exist and IndexError if the +index+ is out of range.
* Returns a value from +self+.
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* With symbol or string argument +name+ given, returns the value for the named member:
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe[:zip] # => 12345
*
* Raises NameError if +name+ is not the name of a member.
*
* With integer argument +n+ given, returns <tt>self.values[n]</tt>
* if +n+ is in range;
* see {Array Indexes}[Array.html#class-Array-label-Array+Indexes]:
*
* joe[2] # => 12345
* joe[-2] # => "123 Maple, Anytown NC"
*
* Raises IndexError if +n+ is out of range.
*
* joe["name"] #=> "Joe Smith"
* joe[:name] #=> "Joe Smith"
* joe[0] #=> "Joe Smith"
*/

VALUE
Expand All @@ -1179,21 +1212,32 @@ rb_struct_aref(VALUE s, VALUE idx)

/*
* call-seq:
* struct[member] = obj -> obj
* struct[index] = obj -> obj
* struct[name] = value -> value
* struct[n] = value -> value
*
* Attribute Assignment---Sets the value of the given struct +member+ or
* the member at the given +index+. Raises NameError if the +member+ does not
* exist and IndexError if the +index+ is out of range.
* Assigns a value to a member.
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* With symbol or string argument +name+ given, assigns the given +value+
* to the named member; returns +value+:
*
* Customer = Struct.new(:name, :address, :zip)
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe[:zip] = 54321 # => 54321
* joe # => #<struct Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=54321>
*
* Raises NameError if +name+ is not the name of a member.
*
* With integer argument +n+ given, assigns the given +value+
* to the +n+th member if +n+ is in range;
* see {Array Indexes}[Array.html#class-Array-label-Array+Indexes]:
*
* joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
* joe[2] = 54321 # => 54321
* joe[-3] = 'Joseph Smith' # => "Joseph Smith"
* joe # => #<struct Customer name="Joseph Smith", address="123 Maple, Anytown NC", zip=54321>
*
* joe["name"] = "Luke"
* joe[:zip] = "90210"
* Raises IndexError if +n+ is out of range.
*
* joe.name #=> "Luke"
* joe.zip #=> "90210"
*/

VALUE
Expand Down
0