On Mon, Jun 2, 2014 at 7:17 AM, Gulys Attila <toraritte / gmail.com> wrote:
> Hi,
>
> I found this method call while following the execution path of a
> simple camping project, and I am not sure whether I interpreted it
> correctly.
>
> class A
>   def initialize(options = nil)
>     @options = options
>   end
>   def options
>     @options ||= {script: 27}
>   end
> end
>
> class B<A
>   def initialize(*)
>     super
>     @lofa = options[:script]
>   end
> end
> [4] pry(main)> B.new
> => #<B:0x007f51b1589490 @options={:script=>27}, @lofa=27>
>
> I couldn't wrap my head around how "options" gets executed when its
> definition does not allow any arguments but rewriting it gets the same
> results.
>
> [14] pry(main)> A.new.options.[](:script)
> => 27
>
> Is it this simple or am I missing something else?

The way this

>     @lofa = options[:script]

works is that the parser checks in this scope for an assigment to a
variable called options. It doesn't find it, so it tags this
occurrence as a method call. When the code is run, the method options
is called with no arguments. Remember that arguments are sent using
parens, not square brackets. The result of that method, then, is sent
the method [] with arguments :script. As has been shown, the result of
the method is either the @options variable if it has been set, or the
hash {:script: 27}.
You can think of the above expression like:

@lofa = options().[](:script)

Hope this helps,

Jesus.