Created
January 24, 2012 22:59
-
-
Save mattbrictson/1673289 to your computer and use it in GitHub Desktop.
Ruby 1.9.2 vs 1.9.3, gotcha with Struct.new and constant scope
This file contains hidden or 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
# Define a simple class with a DEBUG constant | |
# | |
Connection = Struct.new(:name, :state) do | |
DEBUG = false | |
def self.debug? | |
DEBUG | |
end | |
end | |
# A quick sanity check | |
Connection.debug? | |
=> false |
This file contains hidden or 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
# Let's now change the constant from false to true | |
Connection::DEBUG = true | |
# ruby 1.9.2-p180 | |
Connection.debug? | |
=> true | |
# ruby 1.9.3-p0 | |
Connection.debug? | |
=> false |
This file contains hidden or 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
DEBUG = false | |
Connection = Struct.new(:name, :state) do | |
def self.debug? | |
DEBUG | |
end | |
end |
This file contains hidden or 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
# ruby 1.9.2-p180 | |
Connection::DEBUG = true # Actually does DEBUG=true | |
Connection.debug? # Reads new value of DEBUG | |
=> true | |
# ruby 1.9.3-p0 | |
Connection::DEBUG = true # Creates new constant Connection::DEBUG | |
Connection.debug? # Reads unchanged value of DEBUG | |
=> false |
This file contains hidden or 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
Connection = Struct.new(:name, :state) do | |
def self.debug? | |
Connection::DEBUG | |
end | |
end | |
Connection::DEBUG = false |
This file contains hidden or 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
class Connection | |
DEBUG = false | |
attr_accessor :name | |
attr_accessor :state | |
def init(name, state) | |
@name = name | |
@state = state | |
end | |
def self.debug? | |
DEBUG | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read the blog post for a detailed explanation of this Gist: http://blog.55minutes.com/post/16472600410/trivia-constants-in-ruby-1-9-2-vs-1-9-3