summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2013-10-09 23:33:05 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2013-10-09 23:33:05 -0700
commit480e329721097366e784d502686be7ff53ed09b5 (patch)
treef16c41bcc0b6637f6eb6eb097cd1baaf7b63f982
parent7b0baded5a4371c197b4e3adfa82157244207c66 (diff)
parent81c0fda8ecdd6dd4eab8ed64c968d841617df333 (diff)
downloadmruby-480e329721097366e784d502686be7ff53ed09b5.tar.gz
mruby-480e329721097366e784d502686be7ff53ed09b5.zip
Merge pull request #1534 from skandhas/pr-add-some-symbol-methods
Add some methods to Symbol
-rw-r--r--mrbgems/mruby-symbol-ext/mrblib/symbol.rb51
-rw-r--r--mrbgems/mruby-symbol-ext/test/symbol.rb22
2 files changed, 73 insertions, 0 deletions
diff --git a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
index f716162e8..a4e8ef7a8 100644
--- a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
+++ b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
@@ -6,4 +6,55 @@ class Symbol
end
end
+ ##
+ # call-seq:
+ # sym.length -> integer
+ #
+ # Same as <code>sym.to_s.length</code>.
+
+ def length
+ self.to_s.length
+ end
+ alias :size :length
+
+ ##
+ # call-seq:
+ # sym.capitalize -> symbol
+ #
+ # Same as <code>sym.to_s.capitalize.intern</code>.
+
+ def capitalize
+ self.to_s.capitalize.intern
+ end
+
+ ##
+ # call-seq:
+ # sym.downcase -> symbol
+ #
+ # Same as <code>sym.to_s.downcase.intern</code>.
+
+ def downcase
+ self.to_s.downcase.intern
+ end
+
+ ##
+ # call-seq:
+ # sym.upcase -> symbol
+ #
+ # Same as <code>sym.to_s.upcase.intern</code>.
+
+ def upcase
+ self.to_s.upcase.intern
+ end
+
+ #
+ # call-seq:
+ # sym.empty? -> true or false
+ #
+ # Returns that _sym_ is :"" or not.
+
+ def empty?
+ self.to_s.empty?
+ end
+
end
diff --git a/mrbgems/mruby-symbol-ext/test/symbol.rb b/mrbgems/mruby-symbol-ext/test/symbol.rb
index 741315d74..35bb31aef 100644
--- a/mrbgems/mruby-symbol-ext/test/symbol.rb
+++ b/mrbgems/mruby-symbol-ext/test/symbol.rb
@@ -10,3 +10,25 @@ assert('Symbol.all_symbols') do
symbols = Symbol.all_symbols.select{|sym|sym.to_s.include? '__symbol_test'}.sort
assert_equal foo, symbols
end
+
+assert("Symbol#length") do
+ assert_equal 5, :hello.size
+ assert_equal 5, :mruby.length
+end
+
+assert("Symbol#capitalize") do
+ assert_equal :Hello, :hello.capitalize
+ assert_equal :Hello, :HELLO.capitalize
+end
+
+assert("Symbol#downcase") do
+ assert_equal :hello, :hEllO.downcase
+end
+
+assert("Symbol#upcase") do
+ assert_equal :HELLO, :hEllO.upcase
+end
+
+assert("Symbol#empty?") do
+ assert_true :''.empty?
+end