Skip to content

Instantly share code, notes, and snippets.

@audiodude
Created July 20, 2017 22:00
Show Gist options
  • Select an option

  • Save audiodude/eae7ca19712a4f0c4e3bd491d993ad6f to your computer and use it in GitHub Desktop.

Select an option

Save audiodude/eae7ca19712a4f0c4e3bd491d993ad6f to your computer and use it in GitHub Desktop.
class Foo(object):
bar = [1,2,3]
def __init__(self, idx):
self.idx = idx
def my_bar(self):
return self.bar[self.idx]
class Foo
class << self
attr_accessor :bar
bar = [1,2,3]
end
def initialize(idx)
@idx = idx
end
def my_bar
self.class.bar[@idx]
end
end
irb(main):001:0> require './foo'
=> true
irb(main):002:0> f = Foo.new(1)
=> #<Foo:0x007f89560ce1f8 @idx=1>
irb(main):003:0> f.my_bar
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/tmoney/foo.rb:12:in `my_bar'
from (irb):3
from /usr/bin/irb:12:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment