attr_reader, attr_writer, attr_accessor, and initialize

I just wanted to quickly go over the differences between these, because I’d been confused when I first learned them.

Classes in ruby are defined by

class Person
end

Now, when we create an instance of person…

new_person = Person.new

attr_writer

…we might want to give this new person a name.

new_person = Person.new
new_person.name = "Hyung Cho"

However, because we don’t have a writer method to our class, this will give us the following error `<main>': undefined method `name=' for #<Person:0x0000000101ab20> (NoMethodError)

We can create a writer method in two different ways in Ruby. The first is to use attr_writer:

class Person
  attr_writer :name
end

Because writer and reader methods are so commonly used, Ruby has a built in ‘magic’ function that creates them. To write multiple writers or readers, we can separate them with commas.

The second is to define it this way:

class Person
  def name=(name)
    @name = name
  end
end

These methods are identical, and taking a closer look at the second method might give us a better understanding of what our writer method is actually doing. We’re creating a method name= that takes one argument name and setting it to our instance variable @name. So now, when we try setting our name variable to our name, it doesn’t give us an error.

class Person
  attr_writer :name
end

new_person = Person.new
new_person.name = "Hyung Cho"

Remember, new_person.name = "Hyung Cho" is the same thing as new_person.name=("Hyung Cho"). If you take a look at it you’ll see that we’re just calling the name= method!

attr_reader

Now, let’s say that we want to find out the name of our new_person. The obvious way is to try puts new_person.name. However, that won’t work.

When you throw Ruby puts new_person.name, what you’re telling it is to print whatever the method name returns. At this point however, we don’t have the method name defined. This is called a reader method. Whereas the first writer method has us assigning an instance variable through a writer method, our reader method has us calling another method so we can read that instance variable. Once again, we can accomplish this in two ways:

class Person
  attr_writer :name
  attr_reader :name
end

and the other way,

class Person
  def name=(name)
    @name = name
  end

  def name
    @name
  end
end

So now,

class Person
  attr_writer :name
  attr_reader :name
end

new_person = Person.new
new_person.name = "Hyung Cho"

puts new_person.name

will return

>> Hyung Cho

attr_accessor

With both the reader and writer methods understood, we can combine them into a simpler attr_accessor which creates both.

class Person
  attr_reader :name
  attr_writer :name
end

is the same thing as

class Person
  attr_accessor :name
end

Simple, right?!

Initialize

The initialize method is different than the writer and reader methods even though it may seem similar. Think of initialize methods like this: when you call a method that takes in an argument, let’s say for example

def add_two_numbers(num1, num2)
  num1 + num2
end

puts add_two_numbers(1, 2)

>> 3

num1 and num2 get initialized locally. Similarly, setting initialized variables just tells ruby that when an instance of this class gets created, some specific variables will be created. Here’s an example:

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

new_person = Person.new("Hyung Cho")
puts new_person.name

>> Hyung Cho

If you now try to create an instance of the Person class without throwing it an argument like so

class Person
  def initialize(name)
    @name = name
  end
end

new_person = Person.new

it will give you an error `initialize': wrong number of arguments (0 for 1) (ArgumentError)

Don’t be mistaken, an initialized variable does not always need to be thrown as an argument when the object is created. Your initialize method can look something like this:

class Person
  def initialize(name)
    @name = name
    @school = "Crescenta Valley High School"
  end
end

We will never pass in a school argument in the creation of a Person object, but the moment an instance of Person is created, it will automatically set the instance variable @school to "Crescenta Valley High School

There you have it! First time blogging about a technical subject, so any feedback would be appreciated! I tried to explain it in the simplest way possible for beginners to understand.

Hello, world!

Who started this tradition anyway? (“Hello, world!”)

A ton of thoughts have been rolling around my head—some filled with excitement, others with confusion, and the rest with uncertainty. I’ve never been a person who’s able to commit my soul into a risky endeavor, and I suppose it applies to my journey through AppAcademy. I have only my motivation to keep me focused through the long weeks ahead of me.

With that out of the system, I promise to myself and this blog to keep my posts as objective as possible. I’m not certain how often I’d be able to blog new posts especially when class begins, but hopefully quite often!

If you’ve read this far, I guess I can at least introduce myself. My name is Hyung Chul Cho, twenty-two years old, born in South Korea, and moved to California at the age of three. I became quite fond of computers (moreso gaming…) at a young age, and learned my ins and outs of computers by looking over my father’s shoulder whose worked as a freelance web developer ever since I can remember. Economics was my field of interest in high school, but that could possibly be accredited to the fact that our Econ teacher was just the best (Mr. Levering, if you’re out there!), and I quickly fell in love with code during college. Things happened that prevented me from progressing through my courses, and now here I am—hoping to find solid ground through this program.

Now, what is AppAcademy? AppAcademy is a 12-week coding bootcamp held in both San Francisco and New York that teaches web development in a rigorous environment. At the end of the program, they boast a 98% employment rate and an average salary of $105,000 ($85,000 in NY) of which you pay 18% of your first year’s salary as tuition. Not hired? Don’t have to pay. There are certain qualifications to enter this payment program, and in the case you don’t qualify, they offer two alternative ways to pay for your tuition.

I’ve recently been accepted into their August 2015 cohort which starts on the 24th, and life has been hectic ever since. First off, living situation and costs of living. AppAcademy used to graciously offer their facilities to students part of their program to minimize commute time, but they’ve recently pulled their on-campus housing which meant that I’d have to figure out something suitable and fast. Luckily, after a week of searching through Craigslist and Airbnb, I found Inncubator; a startup dedicated to providing short term housing at an affordable cost.

Secondly, prep work. A load of assigned readings, assigned homework, all of which were thrown onto us the moment we got accepted. I’ve finished the initial prep work given on acceptance, but we’re now expected to put in around three hours a day for the next three weeks working on prep exercises. A very interactive side to this is that their TA’s are made to be available for our cohort from 6 to 8 P.M Monday through Thursday to answer questions on the exercises.

Lastly, getting ready to pack. By far the most boring part of prepping.. so we’ll skip this part.

All in all, I remain extremely excited and proud to be a part of this program, and I hope it’ll live up to my expectations. Exactly one month until launch!