Skip to content

Instantly share code, notes, and snippets.

@danwagnerco
Last active August 29, 2015 14:16
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 danwagnerco/54539f72c1f391f7048f to your computer and use it in GitHub Desktop.
Save danwagnerco/54539f72c1f391f7048f to your computer and use it in GitHub Desktop.
Why does self.path = work instead of just path = ?
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