Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Implement to_ary to avoid calls to method_missing #1274

Merged
merged 1 commit into from
Jun 28, 2011
Merged

Implement to_ary to avoid calls to method_missing #1274

merged 1 commit into from
Jun 28, 2011

Conversation

tenderlove
Copy link
Contributor

The following code is calls method missing and emits annoying warnings when -d is used:

class Item
  def method_missing(name, *args)
    p "method_missing(#{name})"
    super
  end
end

p [[Item.new]].flatten

We can silence these warnings by implementing a to_ary that returns nil:

class Item
  def method_missing(name, *args)
    p "method_missing(#{name})"
    super
  end

  private
  def to_ary
    nil
  end
end

p [[Item.new]].flatten

According to ruby spec it's OK for to_ary to return nil, and it will just be ignored. We can also see that the ruby implementation agrees.

This patch adds to_ary for two reasons:

  • Avoiding expensive calls to method_missing
  • Silencing warnings when -d is enabled.

Thanks!

indirect added a commit that referenced this pull request Jun 28, 2011
---

This patch adds `to_ary` for two reasons:

* Avoiding expensive calls to method_missing
* Silencing warnings when -d is enabled.
@indirect indirect merged commit 7c7ffae into rubygems:master Jun 28, 2011
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants