Skip to content

Instantly share code, notes, and snippets.

@ivanacostarubio
Created February 13, 2012 03:37
Show Gist options
  • Select an option

  • Save ivanacostarubio/1813344 to your computer and use it in GitHub Desktop.

Select an option

Save ivanacostarubio/1813344 to your computer and use it in GitHub Desktop.
Comparing Module and Classes Name using < >
#
#
# - Ruby returns false when comparing a Class or Module with itself using the < or > operators.
# - Ruby returns nil when they are not the same.
#
module Z ; end
module X ; end
class C ; end
class V ; end
ruby-1.9.3-p0 :039 > Z > Z
=> false
ruby-1.9.3-p0 :041 > Z < Z
=> false
ruby-1.9.3-p0 :040 > Z > X
=> nil
ruby-1.9.3-p0 :042 > Z < X
=> nil
ruby-1.9.3-p0 :043 > Z < C
=> nil
ruby-1.9.3-p0 :044 > Z > C
=> nil
ruby-1.9.3-p0 :045 > C > C
=> false
ruby-1.9.3-p0 :049 > C > C
=> false
ruby-1.9.3-p0 :046 > C < C
=> false
ruby-1.9.3-p0 :047 > C < V
=> nil
ruby-1.9.3-p0 :048 > C > V
=> nil
#
# Ruby raises TypeError when comparing a Module or Class with a lambda.
#
ruby-1.9.3-p0 :058 > C < lambda {}
TypeError: compared with non class/module
#
# You can see how Rails Core uses this technique at:
# https://github.com/rails/rails/blob/deb91690ae0883826950fa2cfb7c23944d7f03de/activerecord/lib/active_record/model.rb#L18
# + info
# http://ruby-doc.org/core-1.9.3/Comparable.html#method-i-3C
# http://ruby-doc.org/core-1.9.3/Object.html#method-i-3C-3D-3E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment