Created
August 25, 2014 21:48
-
-
Save darrencauthon/81ebc24ffde69d298c09 to your computer and use it in GitHub Desktop.
Overriding the constructor
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 RecordFinder | |
| def initialize username, password | |
| @username = username | |
| @password = password | |
| 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
| RecordFinder.class_eval do | |
| def initialize | |
| @username = ENV['USERNAME'] | |
| @password = ENV['PASSWORD'] | |
| end | |
| end | |
| # now, in my code, I can type: | |
| # | |
| # RecordFinder.new | |
| # | |
| # and in my tests, I can stub it like: | |
| # | |
| # RecordFinder.stubs(:new).returns record_finder | |
| # instead of... | |
| # RecordFinder.stubs(:new).with(username, password).returns record_finder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment