DEV Community

reyes2981
reyes2981

Posted on

Building a CLI for Flatiron Bootcamp pt.2

This is the second part to Building a CLI for Flatiron Bootcamp pt.1

We are going to focus on object oriented programming with Ruby syntax.

To review, per Wikipedia, OOP is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

What the heck does that mean?

It means you can create classes, which is a way to represent objects in your program.

An example would be

class CoolestCanadianActors # this class represents the coolest Canadian actors
  def initialize(first, last) # The initialize method is part of the object-creation process in Ruby & it allows you to set the initial values for an object.
  @first = first
  @last = last
  end
end

obj1 = Name.new("Ryan", "Reynolds") # we've created a new object with the name "Ryan Reynolds". 
print obj1 # prints out object

Enter fullscreen mode Exit fullscreen mode

As stated above, the initialize method is part of the object-creation process and assists in setting initial values for an object. In other programming languages they call this a “constructor”.

Above we created a new object and with the help of the initialize method we were able to use the power of abstraction and create a new "cool Canadian actor".

Please note that Ruby has built in classes. Example would be:

array = Array.new

But since these are built into Ruby we get special syntax to create them.

array = []
hash  = {}
string = ""
Enter fullscreen mode Exit fullscreen mode

Ok, so now that we have our coolestCanadianCelebrity class set up what is next? Well we can add behavior to our objects. We've already done this above with our initialize method but lets dive a little deeper.

To add behavior to our classes we need methods. Methods implement the functionality of your program. Here is an example:

def one_plus_one
1 + 1
end

Note: In Ruby, methods that belong to (are defined on) objects can be used (called) by adding a dot, and then the method name, like so:

coolestCanadianCelebrity.one_plus_one

Alt Text

NOTE: You call the method on an object of coolestCanadianCelebrity, NOT on coolestCanadianCelebrity itself.

Onward and forward!

Lets go back to our code from earlier:

class CoolestCanadianActors
  def initialize(first, last) 
  @first = first
  @last = last
  end
end

obj1 = Name.new("Ryan", "Reynolds")
Enter fullscreen mode Exit fullscreen mode

Remember that our objects can have methods that make things happen, they DO things.

Our initialize method is ran when you create a Ruby object. Within this method you will notice the first, last variables look slightly different from the variables we are used to working with.

@first = first
@last = last

The above are instance variables which are used inside classes and store data, they KNOW things.

Let's say that every time a new CoolestCanadianActors object is created we want it to know how many twitter followers it has. How can I store the number of followers in the CoolestCanadianActors class?

INSTANCE VARIABLES

class CoolestCanadianActors
  def initialize(first, last) 
  @first = first
  @last = last
  @twitter_followers = twitter_followers
  end
end

obj1 = Name.new("Ryan", "Reynolds", "17.7M")
Enter fullscreen mode Exit fullscreen mode

If you look at the above code you'll see our example of a newly created object that is stored in a variable called obj1.

What now?

Well, now that we know how to create classes that represent real world objects we can learn how to access that data. We do that with attribute accessors.

Lets bring back the code we've been working on throughout the article and go over attribute accessors.

class CoolestCanadianActors
  def initialize(first, last) 
  @first = first
  @last = last
  @twitter_followers = twitter_followers
  end
end

obj1 = Name.new("Ryan", "Reynolds", "17.7M")
Enter fullscreen mode Exit fullscreen mode

To access the @first, @last and @twitter_followers instance variables of our Ryan Reynolds object we need methods.

Let's define a getter method so we can access our twitter_followers attribute.

class CoolestCanadianActors

  def initialize(first, last) 
  @first = first
  @last = last
  @twitter_followers = twitter_followers
  end

   def twitter_followers # getter method
    @twitter_followers
   end 

end

obj1 = Name.new("Ryan", "Reynolds", "17.7M")

Enter fullscreen mode Exit fullscreen mode

What if you don't feel like writing methods for each of your attributes? Well have no fear attribute accessors are here.

class CoolestCanadianActors

  attr_accessor first, last, twitter_followers

  def initialize(first, last, twitter_followers)
    @first  = first
    @last = last
    @twitter_followers = twitter_followers
  end

end
Enter fullscreen mode Exit fullscreen mode

attr_accessor is a core feature of Ruby and is used to generate instance variables with getter and setter methods. Note, there are a variety of ways we can utilize attribute_accessor. Let's go back to our updated code so I can show you what I mean:

class CoolestCanadianActors

  attr_accessor :first, :last, :twitter_followers

  def initialize(first, last, twitter_followers)
    @first  = first
    @last = last
    @twitter_followers = twitter_followers
  end

end
Enter fullscreen mode Exit fullscreen mode

What if we wanted to retrieve the read only data inside of the twitter_followers attribute? In others words, the data will be public and accessible to the user.

Well we can write:

attr_reader :twitter_followers

What this means is that we don't need to write a getter method for the object. The attr_reader takes care of the getter's function which is to get a value of an instance variable.

I'm going to wrap it up here. Stay tuned for part III!

Top comments (0)