Created
July 20, 2017 22:00
-
-
Save audiodude/eae7ca19712a4f0c4e3bd491d993ad6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Foo(object): | |
| bar = [1,2,3] | |
| def __init__(self, idx): | |
| self.idx = idx | |
| def my_bar(self): | |
| return self.bar[self.idx] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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