Classes in Ruby Using Getters and Setters

The second week of Code Academy has passed and we’ve learned a number of new concepts. Instead of basically just bulleting everything we learned I’m going to pick one thing and go a little more in depth with it. As I said we learned a number of things but I’d have to say the topic of the week was classes in Ruby. If you’re not familiar with object oriented programming this may or may not confuse a little but continue reading if you wish, I’m not going to explain OOP from the ground up. In short you can think of a class as a prescription slip that a doctor fills out. There are already placeholders for the patient name, the medication prescribed, and the quantity. If that piece of paper was not object oriented the doctor would have to write out “Patient Name: John Doe”, “Prescription: Amoxicillin”, “Drip Count: 14″ instead of just writing John Doe, Amoxicillin, 14. There are many instances of prescriptions you are just filling out a form for this particular instance and because you do this many times over, it’s useful to have some default information.

Now we’ll write a very basic class in Ruby. Typically classes hold a lot of information and that is why it’s useful to use them but for our example we’ll keep it simple. Our goal will be to write a class ‘Prescription’ which will hold information for the prescription name, patient name, and the drip count (quantity).

I’ll use two different examples. In the first example we use getters and setters. These getters and setters will be used to get and set the prescription information (rx name, patient name, drip count). The second example we will be a follow up post where I will refactor our Prescription class and use something called attribute accessors which will allow us to simplify our Prescription class.

Example One: Using getters and setters
class Prescription
	# setter for rx_name
	def rx_name=(value)
		@rx_name = value
	end

	# setter for patient name
	def patient_name=(value)
		@patient_name = value
	end

	# setter for drip count
	def drip_count=(value)
		@drip_count = value
	end

	# getter for rx name
	def rx_name
		puts @rx_name
	end

	# getter for patient name
	def patient_name
		puts @patient_name
	end

	# getter for drip count
	def drip_count
		puts @drip_count
	end
end

rx1 = Prescription.new
rx1.rx_name = "Amoxicillin"
rx1.patient_name = "Joe Dirt"
rx1.drip_count = 30

rx1.rx_name # => Amoxicillin
rx1.patient_name # => Joe Dirt
rx1.drip_count # => 30
Brief Explanation of Example One

We start by defining our class Prescription. Notice that Prescription is capitalized. Classes in ruby must start with an uppercase letter. In Ruby you use def to define a method. The “#” symbol denotes a comment in Ruby. Anything after the # sign on that line will be ignored by the interpreter. Notice how our setters use the = sign directly after defining the method name. That is what makes them setters. The =(value) after the method name allows us to set a value to our instance variable. @rx_name is an example of an instance variable. It makes sense to call our variables “instance” variables because they are just variables for this particular “instance” of a prescription. If we made a new prescription, that new prescription may have a different rx_name.

You can see that on line 33 we created a new instance of our Prescription class by giving that instance a name (rx1) and setting it equal to Prescription.new. This is also known as “instantiating” our Prescription class. We cannot access our Prescription class without having an instance of it (technically you can but that goes beyond this example). After instantiating it we set the rx name to Amoxicillin by calling our rx_name method using .rx_name and repeat that process for patient name and drip count. Notice that Joe Dirt is getting an antibiotic because he is probably dirty and has gotten himself sick.

To recap setters, in lines 34-36 we used our setter methods. When we called rx1.rx_name and set it equal to Amoxicillin, Amoxicillin got passed to our setter method starting on line 3. The interpreter saw that there was a value being set to .rx_name and replaced “value” with “Amoxicillin” and then set @rx_name equal to “Amoxicillin”. So why do we need the getter methods?

We need the getter methods to actually get the value back out after it has been set. Notice that in our getter for rx_name we have “puts @rx_name”. Remember that rx_name is now equal to Amoxcillin for this particular instance of Prescription (rx1), so when we call rx1.rx_name our program will now use the getter method on line 18 instead of the setter method. It knows we are not setting anything because there is no “=”. Puts means just what you think it does. It puts the value onto the screen.

That was a very brief introduction to classes in Ruby and object oriented programming in general. I hope that helped you understand a little bit about both concepts. If you are confused don’t worry, OOP is a pretty heavy topic and this brief example is just an introduction. After I’ve written the follow up post where our class Prescription is refactored and a bit more flexible, I will link to it at the end of this post.

My first week at Code Academy and the move to Chicago

What is Code Academy? A Chicago startup dedicated to teaching aspiring developers how to program and more specifically how to develop with Ruby on Rails. For more info see CodeAcademy.org. Why did I join? I’ve been doing web development for about a year and have been getting paid for it for about 6 months. I [...]

Continue reading...

Bookmarklet that automatically directs you to the WP-Admin.

For my work I often have to access several different WordPress sites everyday. I thought a simple button in my bookmarks toolbar would be a much faster way to get to the admin of any wordpress site than typing in /wp-admin or clicking a login button if there is one available, so I created the [...]

Continue reading...

HTML5 Post

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat [...]

Continue reading...