Skip to content

Instantly share code, notes, and snippets.

@tjmcewan
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjmcewan/a7e4feb2976a93a5eef9 to your computer and use it in GitHub Desktop.
Save tjmcewan/a7e4feb2976a93a5eef9 to your computer and use it in GitHub Desktop.
I dunno...
# http://stackoverflow.com/a/19323467/320438
def plusone(x)
x + 1
end
[1,2,3].map(&:plusone)
~$ ruby -v *[master]
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
~$ irb *[master]
irb(main):001:0> def plusone(x)
irb(main):002:1> x + 1
irb(main):003:1> end
=> :plusone
irb(main):004:0>
irb(main):005:0* [1,2,3].map(&:plusone)
NoMethodError: private method `plusone' called for 1:Fixnum
from (irb):5:in `map'
from (irb):5
from /Users/tim/.rubies/ruby-2.1.2/bin/irb:11:in `<main>'
irb(main):006:0>
~$ ruby -v *[master]
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
~$ pry *[master]
[1] pry(main)> def plusone(x)
[1] pry(main)* x + 1
[1] pry(main)* end
=> :plusone
[2] pry(main)>
[3] pry(main)> [1,2,3].map(&:plusone)
ArgumentError: wrong number of arguments (0 for 1)
from (pry):1:in `plusone'
[4] pry(main)>
@damncabbage
Copy link

Better:

> [1,2,3].map(&"1".method(:foo))
=> [2, 3, 4]

... quiet sobbing

@damncabbage
Copy link

> def foo(x); x + 1; end
=> nil
> method(:foo).owner
=> Object
> 1.method(:foo).owner
=> Object
> 1.class.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
# WELL HELLO, OBJECT.

Well, that solves that mystery.

@ddd1600
Copy link

ddd1600 commented Jun 27, 2014

class Numeric
  def plusone
    self + 1
  end
end

[1,2,3].map(&:plusone)
#=> [2,3,4]

Sorry guys this is all my fault

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment