Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NilClass#[] #102

Closed
trans opened this issue Apr 12, 2014 · 2 comments
Closed

NilClass#[] #102

trans opened this issue Apr 12, 2014 · 2 comments
Labels

Comments

@trans
Copy link
Member

trans commented Apr 12, 2014

class NilClass
  # Simply returns nil.
  #
  # Useful for something like:
  #
  #   if some_hash[:a][:b]
  #   # (where some_hash is a hash of hashes)
  #
  # which would throw a NoMethodError if some_hash didn't have a value for :a.
  #
  # I haven't had any problems using this yet, but it has the possibility of
  # causing some problems with libraries that depend on the other behavior.
  def [](key)
    nil
  end
end

Example of NilClass#[] defined to see the difference.

some_hash = {}

if some_hash[:a][:b]
  puts("got in")
else
  puts("didn't get in")
end
@trans trans added the feature label Apr 12, 2014
@trans
Copy link
Member Author

trans commented Apr 12, 2014

While it would certainly be convenient in many cases, it probably would break a good number of programs too. For this reason I think it has to be rejected.

@ioquatix
Copy link
Contributor

ioquatix commented Mar 9, 2017

This is fixed by ruby's new &. operator.

@ioquatix ioquatix closed this as completed Mar 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants