T
Tsunami Scripter
I've created a class using the following code :
class CommandHandler
attr_accessor :command,:arguments
def initialize(command=nil,arguments=nil,what=nil)
@command = command
@arguments = arguments
@what = what
@code = nil
end
def perform(arguments,what)
@code.call(arguments,what) if [email protected]?
end
end
and a class named CommandProcessor , which , among other methods has the
following :
def add_handler(&block)
handler = CommandHandler.new
handler.instance_eval &block
@map[handler.command] = handler
end
so I can add handlers in a DSL-like way . Here is a sample of code in
action :
add_handler do
command=("get")
code=(lambda do |arguments,what|
begin
@mech.get(arguments.join(""))
return @mech.page.body
rescue
return "#{arguments} could not be loaded"
end
end)
end
end
However , by the time the code reaches @map[handler.command] = handler ,
if I print the handler object , all of it's attributes appear as nil . I
specifically added
command=("...")
and code=("...")
so that the interpreter wouldn't have to use "heuristics" to deduce what
I was trying to say . Why isn't the code working as it should ?
Thanks !
class CommandHandler
attr_accessor :command,:arguments
def initialize(command=nil,arguments=nil,what=nil)
@command = command
@arguments = arguments
@what = what
@code = nil
end
def perform(arguments,what)
@code.call(arguments,what) if [email protected]?
end
end
and a class named CommandProcessor , which , among other methods has the
following :
def add_handler(&block)
handler = CommandHandler.new
handler.instance_eval &block
@map[handler.command] = handler
end
so I can add handlers in a DSL-like way . Here is a sample of code in
action :
add_handler do
command=("get")
code=(lambda do |arguments,what|
begin
@mech.get(arguments.join(""))
return @mech.page.body
rescue
return "#{arguments} could not be loaded"
end
end)
end
end
However , by the time the code reaches @map[handler.command] = handler ,
if I print the handler object , all of it's attributes appear as nil . I
specifically added
command=("...")
and code=("...")
so that the interpreter wouldn't have to use "heuristics" to deduce what
I was trying to say . Why isn't the code working as it should ?
Thanks !