Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is this how you start a hash?

Reply
Thread Tools

Is this how you start a hash?

 
 
Power One
Guest
Posts: n/a
 
      03-19-2009
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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
lists
Guest
Posts: n/a
 
      03-19-2009

On Mar 18, 2009, at 7:50 PM, Power One wrote:

> 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!
> --
> Posted via http://www.ruby-forum.com/.
>
>


Yes, you'd normally create a new has with either Hash.new or hash =
{}. The method above is expecting that an already populated Hash be
passed as its argument (internally referred to as the options
variable). So here's how you'd use the Bot class:

Bot.new(:name => 'My Bot', :data_file => '/tmp/file.yml')

Maybe this helps you see the Hash more clearly:

Bot.new({:name => 'My Bot', :data_file => '/tmp/file.yml'})

or even:

my_options = {:name => 'My Bot', :data_file => '/tmp/file.yml'}
Bot.new(my_options)

Hope this helps.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      03-19-2009
Power One wrote:
>
> 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
>



h = {:name => "Joe", :age => 20}
name = h[:name]
puts name

--output:--
Joe



h = {:age => 30}
name = h[:name] || "Unamed Bot"
puts name

--output:--
Unamed Bot

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Dylan Evans
Guest
Posts: n/a
 
      03-19-2009
[Note: parts of this message were removed to make it a legal post.]

On Thu, Mar 19, 2009 at 10:50 AM, Power One <> wrote:

> 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



Not sure what the author was getting at but YAML#load will probably return a
hash from the data file. The usage of options implies that a hash is passed
as an argument


>
>
> So, when you created hash, don't you have to initialize it with an empty
> hash? For example: @name = {}?



@name looks like it's supposed to be a string which is initialized from the
options hash, the || notation means that if options[:name] doesn't exist
then it will default to "Unnamed Bot".


>
>
> 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



You will need to initialize the hash in this case, or use variables, but it
might be best if your using irb to use constants such as;
@name = "Robocop"



>
>
> 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.



No it's not adding elements to the options hash, it is reading them. That
line returns a hash to @data (which is probably the one you want) based on
the contents of the file referred to by options[:data_file].



>
>
> I'm confused!



Aren't we all?


>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

 
Reply With Quote
 
Power One
Guest
Posts: n/a
 
      03-19-2009
This helps me a lot!

Plus from now on, if something looks like an array but without the index
(ex: options[3]), then I can safely assumed it's a form of hash but in a
reading stage.

Thank guys!

lists wrote:
> On Mar 18, 2009, at 7:50 PM, Power One wrote:
>
>> attr_reader :name
>>
>>
>>

> Yes, you'd normally create a new has with either Hash.new or hash =
> {}. The method above is expecting that an already populated Hash be
> passed as its argument (internally referred to as the options
> variable). So here's how you'd use the Bot class:
>
> Bot.new(:name => 'My Bot', :data_file => '/tmp/file.yml')
>
> Maybe this helps you see the Hash more clearly:
>
> Bot.new({:name => 'My Bot', :data_file => '/tmp/file.yml'})
>
> or even:
>
> my_options = {:name => 'My Bot', :data_file => '/tmp/file.yml'}
> Bot.new(my_options)
>
> Hope this helps.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      03-19-2009

On Mar 18, 2009, at 11:50 PM, Power One wrote:

> This helps me a lot!
>
> Plus from now on, if something looks like an array but without the
> index
> (ex: options[3]), then I can safely assumed it's a form of hash but
> in a
> reading stage.
>
> Thank guys!


No, you can assume that it is the [] method sent to the object
referenced by options.

irb> options = lambda {|x| x.to_s.reverse }
=> #<Proc:0x00007f8ce288b9b0@(irb):1>
irb> options[3]
=> "3"
irb> options["hash"]
=> "hsah"
irb> options[:foo]
=> "oof"

For lambdas or Procs, this is the same as options.call("hash"), etc.

-Rob

Rob Biedenharn http://agileconsultingllc.com


> lists wrote:
>> On Mar 18, 2009, at 7:50 PM, Power One wrote:
>>
>>> attr_reader :name
>>>

>> Yes, you'd normally create a new has with either Hash.new or hash =
>> {}. The method above is expecting that an already populated Hash be
>> passed as its argument (internally referred to as the options
>> variable). So here's how you'd use the Bot class:
>>
>> Bot.new(:name => 'My Bot', :data_file => '/tmp/file.yml')
>>
>> Maybe this helps you see the Hash more clearly:
>>
>> Bot.new({:name => 'My Bot', :data_file => '/tmp/file.yml'})
>>
>> or even:
>>
>> my_options = {:name => 'My Bot', :data_file => '/tmp/file.yml'}
>> Bot.new(my_options)
>>
>> Hope this helps.

> --
> Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      03-23-2009
Power One wrote:
> Plus from now on, if something looks like an array but without the index
> (ex: options[3]), then I can safely assumed it's a form of hash but in a
> reading stage.


Any object can implement its own [] method with whatever semantics it
likes, not just Arrays and Hashes. The [] method is used for lots of
different purposes in the Ruby standard classes:

# Testing individual bits in an integer
irb(main):001:0> 15[2]
=> 1

# Substrings
irb(main):002:0> "abcdefg"["cd"]
=> "cd"
irb(main):003:0> "abcdefg"[/d./]
=> "de"

# Filename globbing
irb(main):004:0> Dir["/etc/*"]
=> ["/etc/fstab", "/etc/X11", "/etc/acpi", "/etc/alternatives",
"/etc/apm" ...]

# Procs
irb(main):005:0> adder = lambda { |x,y| x+y }
=> #<Proc:0xb7d968d8@(irb):7>
irb(main):006:0> adder[4,5]
=> 9

And you can define your own:

class Bot
def [](key)
...
end
def []=(x,y)
...
end
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
ATTN Programmers: WE pay YOU $1.00 if you let us find you a job. f5 Dennis Perl 0 12-02-2003 11:07 AM
ATTN Programmers: WE pay YOU $1.00 if you let us find you a job. f5 Dennis Java 0 12-02-2003 11:07 AM
ATTN Programmers: WE pay YOU $1.00 if you let us find you a job. jdg Doris Cox Perl 0 12-02-2003 11:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57