Skip to content

Commit f08b8ce

Browse files
Saup21claude
authored andcommitted
fix: align core_ext polyfills more closely with active_support implementations
blank?/present?: - String#blank? now uses /\A[[:space:]]*\z/ regex to correctly handle Unicode whitespace (e.g.  ) instead of strip.empty? which is ASCII-only - Add Symbol#blank? aliased to empty? (was missing, active_support covers it) - Array/Hash#blank? use alias_method like active_support instead of explicit def - Object#blank? fallback returns false instead of !self (equivalent in practice but matches active_support intent more clearly) deep_dup: - Hash#deep_dup now starts from dup to preserve hash metadata (default values, default procs, compare_by_identity state); optimises String/Symbol keys to avoid unnecessary key re-insertion - Add Module#deep_dup: named modules return self, anonymous modules are duped Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 74bc9a6 commit f08b8ce

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

lib/pact/support/core_ext.rb

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,32 @@ def present? = true
2121
end
2222

2323
class String
24-
def blank? = empty? || strip.empty?
24+
BLANK_RE = /\A[[:space:]]*\z/
25+
26+
def blank?
27+
empty? || BLANK_RE.match?(self)
28+
end
29+
30+
def present? = !blank?
31+
end
32+
33+
class Symbol
34+
alias_method :blank?, :empty?
2535
def present? = !blank?
2636
end
2737

2838
class Array
29-
def blank? = empty?
39+
alias_method :blank?, :empty?
3040
def present? = !empty?
3141
end
3242

3343
class Hash
34-
def blank? = empty?
44+
alias_method :blank?, :empty?
3545
def present? = !empty?
3646
end
3747

3848
class Object
39-
def blank? = respond_to?(:empty?) ? !!empty? : !self
49+
def blank? = respond_to?(:empty?) ? !!empty? : false
4050
def present? = !blank?
4151

4252
def presence
@@ -62,7 +72,22 @@ def deep_dup
6272

6373
class Hash
6474
def deep_dup
65-
each_with_object({}) { |(k, v), h| h[k.deep_dup] = v.deep_dup }
75+
hash = dup
76+
each_pair do |key, value|
77+
if ::String === key || ::Symbol === key
78+
hash[key] = value.deep_dup
79+
else
80+
hash.delete(key)
81+
hash[key.deep_dup] = value.deep_dup
82+
end
83+
end
84+
hash
85+
end
86+
end
87+
88+
class Module
89+
def deep_dup
90+
name.nil? ? super : self
6691
end
6792
end
6893
end

0 commit comments

Comments
 (0)