#! /usr/bin/ruby

class Foo
   def f
       x = 1
       xdef = defined? x
       puts "is local variable x defined? in Foo method f: #{xdef}"
       @i = 3
       idef = defined? @i
       puts "is instance variable @i defined? in Foo: #{idef}"
       fdef = defined? f
       puts "is method f defined? in Foo: #{fdef}"
   end
end

puts "create a Foo instance and run the f method"
xFoo = Foo.new
xFoo.f

def blah
   puts "there"
end

# see if the global method is defined
blahdef = defined? blah
puts "is global method blah defined?: #{blahdef}"

# see if the class is defined
foodef = defined? Foo
puts "is class Foo defined?: #{foodef}"

# see if the instance is defined
xfoodef = defined? xFoo
puts "is instance xFoo defined?: #{xfoodef}"

# see if the instance has associated method
foores = xFoo.respond_to? 'f'
puts "does xFoo.f respond_to?: #{foores}"

# -------- resulting output ------------
# create a Foo instance and run the f method
# is local variable x defined? in Foo method f: local-variable
# is instance variable @i defined? in Foo: instance-variable
# is method f defined? in Foo: method
# is global method blah defined?: method
# is class Foo defined?: constant
# is instance xFoo defined?: local-variable
# does xFoo.f respond_to?: true

