From 18b45c7ab5d95786a4ef1d960b47754b16c5acf9 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 23 Jun 2025 12:59:15 +0900 Subject: [PATCH] mruby-set: change string representation from # to Set[1] --- mrbgems/mruby-set/README.md | 36 +++++++++++++++++------------------ mrbgems/mruby-set/src/set.c | 10 +++++----- mrbgems/mruby-set/test/set.rb | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/mrbgems/mruby-set/README.md b/mrbgems/mruby-set/README.md index 1fc7ebc82..aea427934 100644 --- a/mrbgems/mruby-set/README.md +++ b/mrbgems/mruby-set/README.md @@ -26,24 +26,24 @@ You can create a set from an array or by using the `Set.[]` shorthand: require 'set' # Not strictly necessary in mruby if compiled in set1 = Set.new([1, 2, 3]) -#=> # +#=> Set[1, 2, 3] set2 = Set[3, 4, 5] -#=> # +#=> Set[3, 4, 5] ``` ### Adding and Deleting Elements ```ruby s = Set.new -s.add(10) #=> # -s << 20 #=> # -s.add?(30) #=> # +s.add(10) #=> Set[10] +s << 20 #=> Set[10, 20] +s.add?(30) #=> Set[10, 20, 30] s.add?(20) #=> nil (20 is already in the set) -s.delete(10) #=> # +s.delete(10) #=> Set[20, 30] s.delete?(5) #=> nil (5 was not in the set) -s.delete?(20) #=> # +s.delete?(20) #=> Set[30] ``` ### Set Operations @@ -56,8 +56,8 @@ s.delete?(20) #=> # set_a = Set[1, 2, 3] set_b = Set[3, 4, 5] -set_a | set_b #=> # -set_a + set_b #=> # +set_a | set_b #=> Set[1, 2, 3, 4, 5] +set_a + set_b #=> Set[1, 2, 3, 4, 5] ``` **Intersection (`&`, `intersection`):** Returns a new set containing elements common to both sets. @@ -66,7 +66,7 @@ set_a + set_b #=> # set_a = Set[1, 2, 3] set_b = Set[3, 4, 5] -set_a & set_b #=> # +set_a & set_b #=> Set[3] ``` **Difference (`-`, `difference`):** Returns a new set containing elements from the first set that are not in the second set. @@ -75,7 +75,7 @@ set_a & set_b #=> # set_a = Set[1, 2, 3] set_b = Set[3, 4, 5] -set_a - set_b #=> # +set_a - set_b #=> Set[1, 2] ``` **Exclusive OR (`^`):** Returns a new set containing elements that are in one or the other of the sets, but not in both. @@ -84,7 +84,7 @@ set_a - set_b #=> # set_a = Set[1, 2, 3] set_b = Set[3, 4, 5] -set_a ^ set_b #=> # +set_a ^ set_b #=> Set[1, 2, 4, 5] ``` ### Querying the Set @@ -164,42 +164,42 @@ s.each { |x| puts x * 10 } ```ruby s = Set[1, 2, 3] -s.map! { |x| x * x } #=> # +s.map! { |x| x * x } #=> Set[1, 4, 9] ``` **Select/Filter (`select!`, `filter!`):** Keeps elements for which the block returns true. ```ruby s = Set[1, 2, 3, 4, 5] -s.select! { |x| x.even? } #=> # +s.select! { |x| x.even? } #=> Set[2, 4] ``` **Reject (`reject!`):** Deletes elements for which the block returns true. ```ruby s = Set[1, 2, 3, 4, 5] -s.reject! { |x| x.odd? } #=> # +s.reject! { |x| x.odd? } #=> Set[2, 4] ``` **Clear (`clear`):** Removes all elements from the set. ```ruby s = Set[1, 2, 3] -s.clear #=> # +s.clear #=> Set[] ``` **Replace (`replace`):** Replaces the contents of the set with the contents of the given enumerable. ```ruby s = Set[1, 2, 3] -s.replace([4, 5]) #=> # +s.replace([4, 5]) #=> Set[4, 5] ``` **Flatten (`flatten`, `flatten!`):** Returns a new set that is a copy of the set, flattening any nested sets. `flatten!` modifies the set in place. ```ruby s = Set[1, Set[2, 3], 4] -s.flatten #=> # +s.flatten #=> Set[1, 2, 3, 4] ``` ## Method Overview diff --git a/mrbgems/mruby-set/src/set.c b/mrbgems/mruby-set/src/set.c index a633c6062..a7b267aa6 100644 --- a/mrbgems/mruby-set/src/set.c +++ b/mrbgems/mruby-set/src/set.c @@ -974,6 +974,7 @@ set_join(mrb_state *mrb, mrb_value self) * set.to_s -> string * * Returns a string representation of the set. + * Format: Set[elem1, elem2, ...] */ static mrb_value set_inspect(mrb_state *mrb, mrb_value self) @@ -984,12 +985,12 @@ set_inspect(mrb_state *mrb, mrb_value self) /* Handle empty set */ if (!kh || kh_size(kh) == 0) { - return mrb_format(mrb, "#<%s: {}>", classname); + return mrb_format(mrb, "%s[]", classname); } /* Handle recursive inspection */ if (mrb_inspect_recursive_p(mrb, self)) { - return mrb_format(mrb, "#<%s: {...}>", classname); + return mrb_format(mrb, "%s[...]", classname); } /* Estimate buffer size based on set size */ @@ -998,9 +999,8 @@ set_inspect(mrb_state *mrb, mrb_value self) /* Create the beginning of the string with pre-allocated capacity */ mrb_value result_str = mrb_str_new_capa(mrb, buffer_size); - mrb_str_cat_lit(mrb, result_str, "#<"); mrb_str_cat_cstr(mrb, result_str, classname); - mrb_str_cat_lit(mrb, result_str, ": {"); + mrb_str_cat_lit(mrb, result_str, "["); /* Iterate through all elements */ mrb_bool first = TRUE; @@ -1023,7 +1023,7 @@ set_inspect(mrb_state *mrb, mrb_value self) } /* Add the closing part */ - mrb_str_cat_lit(mrb, result_str, "}>"); + mrb_str_cat_lit(mrb, result_str, "]"); return result_str; } diff --git a/mrbgems/mruby-set/test/set.rb b/mrbgems/mruby-set/test/set.rb index 329830e02..38826587a 100644 --- a/mrbgems/mruby-set/test/set.rb +++ b/mrbgems/mruby-set/test/set.rb @@ -628,5 +628,5 @@ end # assert("Set#inspect") do set = Set[1,1,1] - assert_equal("#", set.inspect) + assert_equal("Set[1]", set.inspect) end