tags:

views:

77

answers:

3
+2  Q: 

ruby hash question

I was looking at example code from the O'Reilly book on Ruby on Rails and ran across this:

 def label_for(method, options={})
    extra = ""
    if options[:required]
      extra = " <span class='required_mark'>*</span>"
    end
    label(:label || method) + extra + "<br />"
  end

I understand that options is a hash, but how is it able to call "label" with just ":label" -- shouldn't it need to say options[:label] ?

Thanks!

+2  A: 

Yes, I believe so, otherwise the :label symbol will always be passed as a method name to label helper.

khelll
I do believe so too :)
naixn
+1  A: 

It is supposed to be options[:label] unless the author really had something else in mind. Because :label || method is always going return :label which is then passed to the label method.

Chirantan
+1  A: 

You are correct.

label(options[:label] || method) + extra + "<br />"
Spasm
haha, all saying the same here... beaten to the post on a typo :-)Kudos to @khelll and @Chirantan
Spasm