Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 16, 2010 07:31
Show Gist options
  • Select an option

  • Save deepak/743162 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/743162 to your computer and use it in GitHub Desktop.
what would u call defined? in ruby
=begin
<raggi> anyone know if there's a term that describes what the defined? keyword does? 12:44 PM
<raggi> i asked my guys today if they could describe waht it does, and was ofc met mostly with silence, i know i can describe it, but i'm not sure i know of a term that means the same thing 12:44 PM
<dkannan> raggi: type of slot - not a term as such 12:46 PM
<raggi> dkannan: that's not what defined? is 12:46 PM
<raggi> dkannan: try doing: defined?((1;1)) 12:46 PM
<dkannan> raggi: ok 12:47 PM
<dkannan> raggi: some more: defined?(1); defined?(Fixnum); defined?(Object.new); defined?(o = Object.new); 12:51 PM
<dkannan> raggi: it is like how the interpreter sees the world. 12:56 PM
<dkannan> raggi: how does interpreter dry-run sound? 12:56 PM
<raggi> i think the closest term is partial evaluation
=end
ruby-1.9.2-p0 > defined? foo
=> nil
ruby-1.9.2-p0 > defined? foo #not defined
=> nil
ruby-1.9.2-p0 > defined?((1;1))
=> "expression"
ruby-1.9.2-p0 > defined?((1;1))
=> "expression"
ruby-1.9.2-p0 > defined? 1
=> "expression"
ruby-1.9.2-p0 > defined? Fixnum
=> "constant"
ruby-1.9.2-p0 > defined? 1
=> "expression"
ruby-1.9.2-p0 > defined? Object.new
=> "method"
ruby-1.9.2-p0 > defined?(o = Object.new)
=> "assignment"
ruby-1.9.2-p0 > o #o has been defined but it is nil
=> nil
ruby-1.9.2-p0 > o = Object.new
=> #<Object:0x00000001181040>
ruby-1.9.2-p0 > defined?(o)
=> "local-variable"
ruby-1.9.2-p0 > defined? FOO
=> nil
ruby-1.9.2-p0 > FOO
NameError: uninitialized constant Object::FOO
from (irb):9
from /home/deepak/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
ruby-1.9.2-p0 > FOO = 10
ruby-1.9.2-p0 > defined? FOO
=> "constant"
@deepak

deepak commented Dec 16, 2010

Copy link
Copy Markdown
Author

what does the rubyspec say?

@deepak

deepak commented Dec 16, 2010

Copy link
Copy Markdown
Author

defined?(o = Object.new) #assignment
defined?(o) #local-variable

seems like a bug (raggi) as the AST now has the variable 'o'. It is not without side-effects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment