This line:

```
@options ||= {script: 27}
```

Expands out to something like*:

```
if @options
  return @options
else
  @options = {script: 27}
  return @options
end
```

So, the first time you call #options, @options is nil (which is falsy), so
it gets initialized. Then the second time, it's already set, so it gets
used directly.

* actually `foo ||= bar` expands out to something like: `foo || (foo = bar)`


On 2 June 2014 15:17, 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?
>
> regards
> attila
>



-- 
  Matthew Kerwin
  http://matthew.kerwin.net.au/