ruby - Import into local scope -
consider
# sun.rb class sunshine def bright? return true end end def greeting(greeter) puts "hello, sun #{greeter}" end # main.rb def abc my_load "sun.rb" greeting("abc") return sunshine.new end s = abc puts s.bright? greeting("adrian") ...
can have such my_load
here greeting("abc")
call succeeds, latter greeting("adrian")
causes nomethoderror; puts s.bright?
call succeeds.
so, synthetically speaking: such classes,methods sun.rb in scope of caller of my_load
, additionally garbage collected when not referenced anymore?
first off, stand-alone (called on main
object) method call cause nameerror
exception if doesn't exist. nomethoderror
if call method on object.
nothing #=> nameerror class a; end a.nothing #=> nomethoderror
this because when call nothing
on main
, doesn't know if method or variable. however:
nothing() #=> nomethoderror
because ()
knows method trying call. watch out for.
second, if want method work , not work, use undef
.
def greeting(name) puts "hello, #{name}" end greeting("chell") #=> "hello, chell" undef greeting greeting("chell") #=> nomethoderror
Comments
Post a Comment