P
Power One
I'm looking at the code below but still not clear about how this hash
forms! The author of a ruby book I read claimed that the code forms
hash. Is it?
#not complete code, if you try to use it, it won't work!
require 'yaml'
require 'wordplay'
class Bot
attr_reader :name
def initialize(options)
@name = options[:name] || "Unnamed Bot"
begin
@data = YAML.load(File.read(options[:data_file]))
rescue
raise "Can't load bot data"
end
end
end
So, when you created hash, don't you have to initialize it with an empty
hash? For example: @name = {}?
Also when I try to use a snippet in irb:
@name = options[:name] || "Unnamed Bot"
NameError: undefined local variable or method `options' for main:Object
from (irb):1
from :0
options is also in File.read(), it's acting like adding more element to
a hash. Am I right? Though irb tells a different story as if it's not
how you starting a hash.
I'm confused!
forms! The author of a ruby book I read claimed that the code forms
hash. Is it?
#not complete code, if you try to use it, it won't work!
require 'yaml'
require 'wordplay'
class Bot
attr_reader :name
def initialize(options)
@name = options[:name] || "Unnamed Bot"
begin
@data = YAML.load(File.read(options[:data_file]))
rescue
raise "Can't load bot data"
end
end
end
So, when you created hash, don't you have to initialize it with an empty
hash? For example: @name = {}?
Also when I try to use a snippet in irb:
@name = options[:name] || "Unnamed Bot"
NameError: undefined local variable or method `options' for main:Object
from (irb):1
from :0
options is also in File.read(), it's acting like adding more element to
a hash. Am I right? Though irb tells a different story as if it's not
how you starting a hash.
I'm confused!