Created
May 20, 2011 02:15
-
-
Save joshuamiller/982230 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 6255dd5f7263624e36295d21b0180fc88c599621 Mon Sep 17 00:00:00 2001 | |
From: Joshua Miller <josh@joshinharrisburg.com> | |
Date: Thu, 19 May 2011 19:13:28 -0700 | |
Subject: [PATCH] Add #<=> and #casecmp to Symbol | |
--- | |
kernel/common/symbol.rb | 19 +++++++++++++++++++ | |
1 files changed, 19 insertions(+), 0 deletions(-) | |
diff --git a/kernel/common/symbol.rb b/kernel/common/symbol.rb | |
index 42369bd..a01d3f7 100644 | |
--- a/kernel/common/symbol.rb | |
+++ b/kernel/common/symbol.rb | |
@@ -38,6 +38,25 @@ class Symbol | |
alias_method :intern, :to_sym | |
alias_method :id2name, :to_s | |
+ | |
+ ## | |
+ # Comparison --- Returns -1 if <i>other</i> is less than, 0 if | |
+ # <i>other</i> is equal, and +1 if <i>other</i> is greater than | |
+ # <i>self</i>. Comparison is provided by <code>String#<=></code>. | |
+ def <=>(other) | |
+ return unless other.respond_to?(:to_sym) && other.respond_to?(:<=>) | |
+ self.to_s <=> other.to_sym.to_s | |
+ end | |
+ | |
+ ## | |
+ # Case-insensitive version of <code>Symbol#<=></code>. Similarly | |
+ # relies on <code>String#casecmp</code> using string representations | |
+ # of <i>self</i> and <i>other</i> | |
+ def casecmp(other) | |
+ return unless other.respond_to?(:to_sym) && other.respond_to?(:<=>) | |
+ self.to_s.casecmp(other.to_sym.to_s) | |
+ end | |
+ | |
# Returns a Proc object which respond to the given method by sym. | |
def to_proc | |
# Put sym in the outer enclosure so that this proc can be instance_eval'd. | |
-- | |
1.7.0.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why is there a check for '&& other.respond_to?(:<=>)' in #<=> ? self.to_s is sent #<=>
Also, why is there '&& other.respond_to?(:<=>)' in #casecmp ?