Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Last active April 14, 2017 01:02
Show Gist options
  • Select an option

  • Save benkoshy/b18e43070df0d37e6a352299147a3664 to your computer and use it in GitHub Desktop.

Select an option

Save benkoshy/b18e43070df0d37e6a352299147a3664 to your computer and use it in GitHub Desktop.
Hide data structures and send messages
# consider this person class:

class Person
	attr_reader :data
	def initialize(data)
		@data = data
	end
end

# our programmer wants to pass in an array of numbers
# he wants the first number to reflect the person's
# age and the second number to represent the person's weight.


p = Person.new([35, 150])

# now let's access this person's age.
age = p.data[0]

# now let's get his/her weight
weight = p.data[1]

# now let's create another person
# and access their age and weight

p1 = Person.new([50, 110])
p1_age = p1.data[0]
p1_weight = p1.data[1]


# what is wrong with the above code?
# please make an attempt about thinking about the 
# consequences of coding like this, before continuing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment