Last active
August 29, 2015 14:16
-
-
Save danwagnerco/54539f72c1f391f7048f to your computer and use it in GitHub Desktop.
Why does self.path = work instead of just path = ?
This file contains 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 DateType | |
attr_reader :entry | |
attr_accessor :path, :error_state | |
def initialize(entry) | |
@entry = entry.to_s | |
@path = "" | |
@error_state = false | |
end | |
def prepare_path_that_does_not_update_the_instance_variables | |
case | |
when entry.downcase.include?("tra") | |
path = "Transaction" | |
when entry.downcase.include?("pro") | |
path = "Process" | |
when entry.downcase.include?("cre") | |
path = "CreatedOn" | |
else | |
error_state = true | |
end | |
end | |
def prepare_path_that_updates_the_instance_variables | |
case | |
when entry.downcase.include?("tra") | |
self.path = "Transaction" | |
when entry.downcase.include?("pro") | |
self.path = "Process" | |
when entry.downcase.include?("cre") | |
self.path = "CreatedOn" | |
else | |
self.error_state = true | |
end | |
end | |
end | |
example_that_fails = DateType.new("tra") | |
example_that_fails.prepare_path_that_does_not_update_the_instance_variables | |
example_that_fails.path # => "", but I expected to get "Transaction" | |
example_that_works = DateType.new("tra") | |
example_that_works.prepare_path_that_updates_the_instance_variables | |
example_that_works.path # => "Transaction", why do I need to include self? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment