From 6f1864ec73b6e494addc0ace387bd1c6b0b11497 Mon Sep 17 00:00:00 2001 From: Julien Silland Date: Thu, 5 Sep 2024 11:41:01 -0700 Subject: [PATCH 1/2] Adds documentation for message repeated fields The documentation is currently lacking an example for how to handle repeated fields of messages. This changes proposes an addition which documents how to construct instances of `RepeatedField` which contain submessages --- content/reference/ruby/ruby-generated.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/content/reference/ruby/ruby-generated.md b/content/reference/ruby/ruby-generated.md index c708386d..da5b7969 100644 --- a/content/reference/ruby/ruby-generated.md +++ b/content/reference/ruby/ruby-generated.md @@ -274,6 +274,22 @@ int_repeatedfield.clear raise unless int_repeatedfield.empty? ``` +For repeated fields that contain messages the constructor for +`Google::Protobuf::RepeatedField`, support a variant with three arguments: +`:message`, the class of the submessage, and the values to set: + +```ruby +first_message = MySubMessage.new(:foo => 42) +second_message = MySubMessage.new(:foo => 79) + +repeated_field = Google::Protobuf::RepeatedField.new( + :message, + MySubMessage, + [first_message, second_message] +) +message.sub_message_repeated_field = repeated_field +``` + The `RepeatedField` type supports all of the same methods as a regular Ruby `Array`. You can convert it to a regular Ruby Array with `repeated_field.to_a`. From 99b14af2e7e36be9fb1ff29c884b7a3bf9c84064 Mon Sep 17 00:00:00 2001 From: Julien Silland Date: Fri, 6 Sep 2024 09:13:35 -0700 Subject: [PATCH 2/2] Update ruby-generated.md --- content/reference/ruby/ruby-generated.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/reference/ruby/ruby-generated.md b/content/reference/ruby/ruby-generated.md index da5b7969..d4af5b57 100644 --- a/content/reference/ruby/ruby-generated.md +++ b/content/reference/ruby/ruby-generated.md @@ -274,8 +274,8 @@ int_repeatedfield.clear raise unless int_repeatedfield.empty? ``` -For repeated fields that contain messages the constructor for -`Google::Protobuf::RepeatedField`, support a variant with three arguments: +For repeated fields that contain messages, the constructor for +`Google::Protobuf::RepeatedField` supports a variant with three arguments: `:message`, the class of the submessage, and the values to set: ```ruby