summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorKOBAYASHI Shuji <[email protected]>2019-02-02 18:53:49 +0900
committerKOBAYASHI Shuji <[email protected]>2019-02-02 18:53:49 +0900
commit668ed605219706cbc9b45f21752fe471dd368e02 (patch)
tree47738e74a8486820288e06706436c1473eaec5ac /lib
parent32067b500592d10a6458cdb273c42a5ebdb4fbf9 (diff)
downloadmruby-668ed605219706cbc9b45f21752fe471dd368e02.tar.gz
mruby-668ed605219706cbc9b45f21752fe471dd368e02.zip
Extend only when necessary in `lib/mruby-core-ext.rb`
Diffstat (limited to 'lib')
-rw-r--r--lib/mruby-core-ext.rb40
1 files changed, 23 insertions, 17 deletions
diff --git a/lib/mruby-core-ext.rb b/lib/mruby-core-ext.rb
index 4c6d3ca76..08e6f6148 100644
--- a/lib/mruby-core-ext.rb
+++ b/lib/mruby-core-ext.rb
@@ -18,18 +18,20 @@ class String
end
# Compatible with 1.9 on 1.8
- def %(params)
- if params.is_a?(Hash)
- str = self.clone
- params.each do |k, v|
- str.gsub!("%{#{k}}") { v }
- end
- str
- else
- if params.is_a?(Array)
- sprintf(self, *params)
+ unless (sprintf("%{a}", :a => 1) rescue false)
+ def %(params)
+ if params.is_a?(Hash)
+ str = self.clone
+ params.each do |k, v|
+ str.gsub!("%{#{k}}") { v }
+ end
+ str
else
- sprintf(self, params)
+ if params.is_a?(Array)
+ sprintf(self, *params)
+ else
+ sprintf(self, params)
+ end
end
end
end
@@ -37,17 +39,21 @@ end
class Symbol
# Compatible with 1.9 on 1.8
- def to_proc
- proc { |obj, *args| obj.send(self, *args) }
+ unless method_defined?(:to_proc)
+ def to_proc
+ proc { |obj, *args| obj.send(self, *args) }
+ end
end
end
module Enumerable
# Compatible with 1.9 on 1.8
- def each_with_object(memo)
- return to_enum :each_with_object, memo unless block_given?
- each { |obj| yield obj, memo }
- memo
+ unless method_defined?(:each_with_object)
+ def each_with_object(memo)
+ return to_enum :each_with_object, memo unless block_given?
+ each { |obj| yield obj, memo }
+ memo
+ end
end
end