From: Rob Biedenharn Date: 2009-03-19T14:31:06+09:00 Subject: Re: Is this how you start a hash? 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 } => # 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 Rob@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/.