[前][次][番号順一覧][スレッド一覧]

ruby-changes:66480

From: jfrazx <ko1@a...>
Date: Mon, 14 Jun 2021 22:53:36 +0900 (JST)
Subject: [ruby-changes:66480] 931ea7cfbe (master): Add fallback block to `OpenStruct#delete_field` (#1409)

https://git.ruby-lang.org/ruby.git/commit/?id=931ea7cfbe

From 931ea7cfbec6d863cd8b48308804323704a2696c Mon Sep 17 00:00:00 2001
From: jfrazx <jfrazx@u...>
Date: Mon, 14 Jun 2021 08:53:20 -0500
Subject: Add fallback block to `OpenStruct#delete_field` (#1409)

---
 lib/ostruct.rb               | 11 +++++++++--
 test/ostruct/test_ostruct.rb | 12 +++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 1462901..b3ff6ef 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -326,8 +326,10 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L326
   end
 
   #
-  # Removes the named field from the object. Returns the value that the field
-  # contained if it was defined.
+  # Removes the named field from the object and returns the value the field
+  # contained if it was defined. You may optionally provide a block. 
+  # If the field is not defined, the result of the block is returned, 
+  # or a NameError is raised if no block was given.
   #
   #   require "ostruct"
   #
@@ -341,6 +343,10 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L343
   #   person.pension = nil
   #   person                 # => #<OpenStruct name="John", pension=nil>
   #
+  #   person.delete_field('number')  # => NameError
+  #
+  #   person.delete_field('number') { 8675_309 } # => 8675309
+  #
   def delete_field(name)
     sym = name.to_sym
     begin
@@ -348,6 +354,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L354
     rescue NameError
     end
     @table.delete(sym) do
+      return yield if block_given?
       raise! NameError.new("no field `#{sym}' in #{self}", sym)
     end
   end
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 7773da4..f8d184b 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -89,7 +89,7 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L89
     a = o.delete_field :a
     assert_not_respond_to(o, :a, bug)
     assert_not_respond_to(o, :a=, bug)
-    assert_equal(a, 'a')
+    assert_equal('a', a)
     s = Object.new
     def s.to_sym
       :foo
@@ -100,6 +100,16 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L100
     o.delete_field s
     assert_not_respond_to(o, :foo)
     assert_not_respond_to(o, :foo=)
+
+    assert_raise(NameError) { o.delete_field(s) }
+    assert_equal(:bar, o.delete_field(s) { :bar })
+
+    o[s] = :foobar
+    assert_respond_to(o, :foo)
+    assert_respond_to(o, :foo=)
+    assert_equal(:foobar, o.delete_field(s) { :baz })
+
+    assert_equal(42, OpenStruct.new(foo: 42).delete_field(:foo) { :bug })
   end
 
   def test_setter
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]