---
title: "Six Year Old Optional / Keyword Arguments bug"
description:
  "This blog discusses a Six Year Old Optional / Keyword Arguments bug that was
  fixed recently in Ruby 2.2"
canonical_url: "https://www.bigbinary.com/blog/six-years-old-optional-keyword-arguments-bug"
markdown_url: "https://www.bigbinary.com/blog/six-years-old-optional-keyword-arguments-bug.md"
---

# Six Year Old Optional / Keyword Arguments bug

This blog discusses a Six Year Old Optional / Keyword Arguments bug that was
fixed recently in Ruby 2.2

- Author: Vipul
- Published: April 28, 2014
- Categories: Rails, Ruby

I recently conducted a workshop about **_Contributing to Open-Source_** at
first-ever [Rubyconf Philippines](http://rubyconf.ph/). In its introductory
talk, I spoke about how Aaron Patterson, fixed
[a 6 year old bug](https://github.com/rails/rails/commit/e88da370f190cabd1e9750c5b3531735950ab415)
about Optional Arguments, that existed in Rails.

## Bug in ruby

Let's try a small program.

```ruby

class Lab

def day
puts 'invoked'
'sunday'
end

def run
day = day
end

end

puts Lab.new.run

```

What do you think would be printed on your terminal when you run the above
program.

If you are using ruby 2.1 or below then you will see nothing. Why is that ?
That's because of a bug in ruby.

This is [bug number 9593](https://bugs.ruby-lang.org/issues/9593) in ruby issue
tracker.

In the statement `day = day` the left hand side variable assignment is stopping
the call to method `day`. So the method `day` is never invoked.

## Another variation of the same bug

```ruby

class Lab

def day
puts 'invoked'
'sunday'
end

def run( day: day)
end

end

puts Lab.new.run

```

In the above case we are using the keyword argument feature added in Ruby 2.0 .
If you are unfamiliar with keyword arguments feature of ruby then checkout this
[excellent video](https://www.youtube.com/watch?v=u8Q6Of_mScI) by
[Peter Cooper](https://twitter.com/peterc).

In this case again the same behavior is exhibited. The method `day` is never
invoked.

## How this bug affects Rails community

You might be thinking that I would never write code like that. Why would you
have a variable name same as method name.

Well Rails had this bug because rails has code like this.

```ruby

def has_cached_counter?(reflection = reflection)
end

```

In this case method `reflection` never got called and the variable `reflection`
was always assigned nil.

## Fixing the bug

[Nobu](https://bugs.ruby-lang.org/users/4)
[fixed](https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/45272)
this bug in ruby 2.2.0. By the way Nobu is also known as "ruby patch-monster"
because of amount of patches he applies to ruby.

So this bug is fixed in ruby 2.2.0. What about the people who are not using ruby
2.2.0.

The simple solution is not to omit the parameter. If we change the above code to

```ruby

def has_cached_counter?(reflection = reflection())
end

```

then we are explicitly invoking the method `reflection` and the variable
`reflection` will be assigned the output of method `reflection`.

And this is how
[Aaron Patterson fixed](https://github.com/rails/rails/commit/e88da370f190cabd1e9750c5b3531735950ab415)
six years old bug.

## Links

- [Human page](https://www.bigbinary.com/blog/six-years-old-optional-keyword-arguments-bug)
