Created
January 19, 2018 18:45
-
-
Save maxjacobson/388d906d82aee626cc0e09d57a2328f7 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
lvar = 1 | |
defined?(lvar) # => "local-variable" | |
defined?(no_lvar) | |
$gvar = 1 | |
defined?($gvar) # => "global-variable" | |
defined?($no_gvar) # => nil | |
CONST = 1 | |
defined?(CONST) # => "constant" | |
defined?(NO_CONST) | |
OTHER_CONST = nil | |
defined?(OTHER_CONST) | |
module Mod; end | |
defined?(Mod) # => "constant" | |
class Klass; end | |
defined?(Klass) # => "constant" | |
module A | |
class B | |
end | |
defined?(B) # => "constant" | |
end | |
defined?(A) # => "constant" | |
defined?(A::B) # => "constant" | |
defined?(B) # => nil | |
def m; end | |
defined?(m) # => "method" | |
@ivar = 1 | |
defined?(@ivar) # => "instance-variable" | |
defined?(@no_ivar) # => nil | |
def foo? | |
end | |
defined?(foo?) # => "method" | |
defined?(4.+(4)) # => "method" |
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
def our_defined?(ident, outer_binding) | |
if outer_binding.local_variables.include?(ident) | |
"local-variable" | |
elsif global_variables.include?(ident) | |
"global-variable" | |
else | |
binding.irb if ident == :@ivar | |
end | |
end | |
lvar = 1 # => 1 | |
our_defined?(:lvar, binding) # => "local-variable" | |
our_defined?(:no_lvar, binding) # => nil | |
$gvar = 1 | |
our_defined?(:$gvar, binding) # => "global-variable" | |
our_defined?(:$no_gvar, binding) # => nil | |
CONST = 1 | |
our_defined?(:CONST, binding) # => nil | |
our_defined?(:NO_CONST, binding) # => nil | |
OTHER_CONST = nil | |
our_defined?(:OTHER_CONST, binding) # => nil | |
module Mod; end | |
our_defined?(:Mod, binding) # => nil | |
class Klass; end | |
our_defined?(:Klass, binding) # => nil | |
module A | |
class B | |
end | |
our_defined?(:B, binding) # => nil | |
end | |
our_defined?(:A, binding) # => nil | |
our_defined?(:"A::B", binding) # => nil | |
our_defined?(:B, binding) # => nil | |
def m; end | |
our_defined?(:m, binding) # => nil | |
@ivar = 1 | |
our_defined?(:@ivar, binding) # => nil | |
our_defined?(:@no_ivar, binding) # => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment