0

I'm farily new to the ruby language and I came across this line of code:

f.options[:chart][:defaultSeriesType] = "bar"  

Could somebody please explain that one to me? Because doing this:

f.options([:chart][:defaultSeriesType]) = "bar"  

Gives you an error. Thanks in advance!

2
  • syntax error, unexpected '=', expecting keyword_end f.option([:chart][:defaultSeriesType]) = "bar" Commented Aug 30, 2011 at 14:02
  • It's because f.options it's an hash ... of course such syntax it's not legal for hash object. Commented Aug 30, 2011 at 14:07

3 Answers 3

3

f.options should be a hash, like so

f.options = {:chart => {}}

Then the command you first wrote will work. So it is not a method call, but actually setting the value of a hash.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

You are handling with an hash.
When you have doubts like that do f.options.inspect, it will print out the content of the data structure.
I'll let you understand with an example:
Based from the way you wrote, it seems that you are handling an object organized more on less in this way :

f.options = { chart => {defaultSeriesType => "bar"; somethingElse => "bor"}, graph => {attribute1=> "anotherString"} }

So you can query the object by writing: f.options[:graph][:attribute1] or f.options[:chart][:somethingElse] and so on.
I suggest you to spend one minute on http://www.tryruby.org and playing with the hash, you could also take a look here: http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Hashes

Sure it helps

Comments

1

Whenever you have questions like this, open up the console and play with the objects

>f.options.class
=> Hash
>f.options[:chart].class
=> Hash

f.options[:chart] is returning a Hash. So, the line f.options[:chart][:defaultSeriesType] = "bar" is setting the value for a hash with key as 'defaultSeriesType' and value as 'bar'.

And, it's a good practice to use a symbol instead of string for keys, hence the colon at the front - :defaultSeriesType

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.