Webinar: Understanding Ruby Classes
So in Ruby you'll often hear that everything in Ruby or almost everything in Ruby is an object and so there's a lot of neat ways that neat things that you can do with breaking everything down to an object and understanding how Ruby really functions as an object oriented programming.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So before we really jump into things too heavily today I wanted to go ahead and just go over some examples. And kind of show some neat things that you can do from within Ruby. So one of the first things I'll do is right here. Let's see. Some reason it doesn't want to let me run this. OK.

So let's see. And before I get started Can everyone see my screen. OK just wanted to make sure. Perfect. OK. So what I'm going to go ahead and do is we'll create a quick sting. I'll just call this "hello world". And then also create a quick array. And we could have Ruby print this out.

Yeah. For some really want me switch for some reason my replica isn't working super well let me switch to a different window. Cause I know that will work. inside my saved. Sorry just some minor technical difficulties. Okay for some reason replit doesn't look like it's going to work so we'll just use this file that I had open and it does ultimately the same thing as replit so we'll go in. I'll create my string of "hello world". And then will also create our array. And now we should be able to tell that to run. So there. Now we have where it's printing our string Hello World and it's printing our array 1 2 3 4 5.

puts "hello world"
puts [1, 2, 3, 4, 5]

But a really neat thing inside of Ruby a built in method is actually going to let us to understand more about what this is. So for example if I wasn't sure that hello world was a string I could use .class which has a predefined Ruby method. At the end of hello world and at the end of my 1 2 3 4 5. So as I do that I'll quickly save it. And then what happens next is when we run it Ruby will actually tell me that the first one is a string and the next one is an array.

puts "hello world".class
puts [1, 2, 3, 4, 5].class

And as you continue to break it down. You could also use a built in method that is called superclass. And it will break it down a little further for you. So now we can see that the class of string and the class of array are also objects. So these are really neat because as you kind of dive into these you can better decipher how to manage and how you can use all your different variables or so forth inside of Ruby. A great thing to do is let's go ahead and actually backtrack a little bit.

puts "hello world".class.superclass
puts [1, 2, 3, 4, 5].class.superclass

So we have this array of one two three four five but if we don't necessarily know it's an array first we can run it. And now that we have it's an array we can come into ruby-doc.org right here and I'll go ahead and post this link in the chat. Just so everyone can have it. If you're not using Ruby docs as a resource definitely make it a habit because this is some incredible documentation where it gives you very detailed examples and so forth of how you can use each of the different classes and the different methods with inside of Ruby so what we'll do is we know that this is an array so we'll come over here to the classes and go to array and we can now learn all about different arrays and so forth.

Does that make sense to everyone. Are there any questions so far. yeah go ahead. No. It makes sense. Okay. So this is super neat. So it's just different things you can use for and use and so now what I'm going to do is we'll take our string and I want to show you guys something really neat so all actually called Class One more time on top of each of these so I'm going to take string and array and then call the class method again that its built in.

puts "hello world".class.class
puts [1, 2, 3, 4, 5].class.class

And as you see now these are both classes so by default and Ruby an array and a string are already built in classes and so it's important to understand that because a lot of what you do whether you recognize it or not inside of Ruby will come down to working with in some sort of class. A lot of what you do in Ruby or sorry in Rails is working with classes for example when you're creating new blogs or so forth. You're just creating new instances and instantiating over and over again different. Classes of blog or a user or so forth. So this, that class is a neat thing that you can use to break down things further and you can use a superclass to break it down to see that it is an object.

Which is kind of it was just fun for me when I discovered that because it was really when I was like Oh okay so that's what they mean when everything is an object inside Ruby. And the neat thing is you can do this not just with variables or strings you can actually do it with a class so what it will do now is I will go ahead and create a new class so let's call this class just hello. And I'll give it a method of creating. And call hello world.

class Hello
   def greeting
      "hello world"
   end
end

So basic basic setup of the classes when you're naming it you'll always use camel case. So this is. Hello. Hello is going to be capitalized. And if I was doing a second word I could call a Hello World and uppercase the first letter of the new word as well. And now when we're calling a class to to run what we'll do is we'll we have to instantiate the class and call the class again so we will call this hi. And you can call this variable anything you'd like but now we'll go ahead and say hello.new.

class Hello
   def greeting
      "hello world"
   end
end

hi = Hello.new
puts hi.greeting

And then we'll take Hi. Because hi is now an instance of the class hello. And I will take hi and ask get to run the method that I created in here called greeting. And then before I do that I'll just right. Put the puts before and so now print out the hello world. So we'll come back in here and then my terminal will run Ruby and you can see right here that it printed out my hello world.

Now what I wanted to show you is that just like before we can go ahead and actually break it down using the same Ruby methods. That we used before with a .class and the .superclass. So I will say puts Hello.class. And as we run it again you'll see that it's a class that hello is a class and when we run Super class. They'll actually return that it's a module and all get into modules a little deeper in a future class. And then if we want run super superclass one more time

class Hello
   def greeting
      "hello world"
   end
end

puts Hello.class.superclass.superclass

You'll see that this class of Hello is ultimately just an object. So that's another example of how you can really break it down and use this dot class superclass on just about anything including. The class itself. One other really neat thing that I Will occasionally use for all of these. Let's go back to the beginning. I'll actually comment these out. And if you are coding all trying to code a longer follow along don't worry I am actually going to post all of this. Code and make it available to everyone after the class. You don't have to. Get too stressed out. If I'm going too fast.

But if there is any questions feel free to go ahead and post in that chat or just mute yourself and let me know because we don't want anyone to feel lost. But I'm going to come back and we will call the same string I did before but will we're going to define it with a variable of a. I'm also going to call it let's call this s for string a for array

So I defined a variable just to make it a little easier to work with. But now we can run the same thing as before. So a.class will get back that it is an array. So. Now I've got to tell it to print or run my puts. So you see that it's an array but a really neat thing that I'll use on my array and my strings and so forth I'll actually call this use the a.methods. And this is another pre-built Ruby method. So when we run this it'll print out all the available methods to.

s = "Hello World"
a = [1, 2, 3, 4, 5]

puts a.methods

To this variable or this array. So now if I wanted to to further. Kind of troubleshoot or be able to build out a certain feature in my application is I can come in here I can read some of these like my flatten and I know that it's that flat and will now be a method available to research. Inside of my ruby dock. So if I come here flatten array and I can read up on what Flatten does. So this is it does print out all the methods available so it's not something you need to run every single time but it's a tool that definitely comes in handy again when you're trying to further troubleshoot and debug your application.

So an important thing and I actually stole this from someone online because I thought it was a really good example of how to kind of understand a class and how how it kind of manages each of these different methods. So the example they put is, k so where are all these methods coming from. And and then they said okay classes are like umbrella's they let us give an object. General behaviors based on what it is. So an object is always just an instance of a class as you saw when I ran this code up here.

And so it's a good example to think about is a person or you yourself you have the class of person as a person there's lots of behaviors that you could do such as laugh, jump, speak, write and so forth. Eat. And then. On top of that you could also be part of the class of a living thing. All living things might share that these abilities or these methods in common. So as you're breaking each thing down. Into little pieces an array a string it's all just a member of a class that has available certain methods or certain behaviors that it can do just like a person or an individual.

So to better understand classes let's go ahead and just start building our game that I've kind of. Came up with for today and if we have enough time in the end I'll actually ask for a lot of use or purchase a patient so we can kind of build the game together as I built out the initial aspect of the game to kind of give you guys a better example of how classes and variables work within classes. But then for fun if there is time I think would be cool to just have everyone kind of participating get some ideas. To go ahead and get started. We'll create a class of game.

And it looks like an auto corrected to auto field something for us. Okay. So who can tell me what is an important rule for naming a class. I went over it a little earlier but I want to see if anyone everyone was listening. Within a class or methods but you're asking about. The rule. Yes. So what's the rule for naming a class.

Because if you name it Disneyland and you don't it is always talking about. So two people started talking, Michael what. What were you saying. You explain a while ago that like if you name it. Something that you don't. That doesn't relate to what you're. What the methods are their functions are then you don't know. It's. You don't know what you're referring to. That's close. I mean that is a great principle to go by.

I mean you're right I wouldn't want to name the classic game you know mountain because for this or the game itself mountain won't make a lot of sense. So you do want to be very clear and specific in how you name, someone else was talking. You want to. It was. Hartman You want to us camel case, you said to use camel case to name a class with each separated word. Exactly so camel case again first letter of each new word is going to be capitalized so game. In this case since it's just one word G is capitalized. If I called this game one the o and one would be capitalized.

OK. Yes create feedback from everyone so now. Now with the class created. I want to introduce to everyone and you've probably seen this in a few applications or online tutorials especially if you try to research more on classes itself but it's a really neat method that. Ruby actually has built into itself. That's initialize so what we'll do. Is create. This method of initialize and what the site's going to do is it's going to tell our application just to run this before getting any further into the game and I'll show you a couple of ways that we can initialize aspects of the class.

class Game
   def initialize

   end
end

But first let's start with just one of them that I'll just hard code and so will call those names. I will call it James. And we'll be building a rival and toward game so I'll just call it rival and I'm creating it. Each of these variables I'm putting in @ sign. Does anyone remember from last week what the at sign represents. Class variable. Close the class variable would be the. Instance instance correct. Yes so an instance variable what it's saying is it's just making this available throughout the rest of the class and other methods. And. I like to think of it or explain it like a key to a door.

If an instance variable is kind of it's more. Accessible to unlock every door inside of the building where a local variable would only unlock that one door. So for example if I just put key = "my key" The variable Key is only available inside of the method initialize and that's just because it's local to just that method. Adding that @ sign or creating it to be an instance variable will allow these variables name and rival to be available elsewhere throughout the application.

So with this I'm going to quickly create one more method just so we can see what. Happened within initialize. So we'll just call this greeting again and we'll just say puts. Hello and then. Go ahead and. Have in the variables. Okay so this is using string interpellation. Is there anyone that's not familiar with this syntax. That I've setup here to call the variables. So string interpellation is that pound sign then the parentheses with the instance variable inside this code.

class Game
   def initialize
      @name = "James"
      @rival = "Tyson"
   end

   def greeting
      puts "Hello #{name}. meet your rival #{rival}"
   end
end

Yes so what's that. What's that. And you'll see it in just a moment. But what this is doing is it's allowing this string to go and actually pull. The. Variable up here and print out the variable as part of the string. If we were just to do something like this all we would get it Ruby to print out is hello at name. So by setting it up this way. By interpellate it will actually let us use and pass the value of @name into our sentence. So its kind of like our Mad lib. Exactly.

Okay cool. So now what we're going to go ahead and do is with this basic class setup. We'll go ahead and we'll quickly call. So I will just create a new variable or a new instance of game and we'll just call this new_game and I will call game.new. And then new_game.greeting. Okay and can someone answer so this new game. Does it have to be called new game or what. What else could we have called.

Well it's not it's not relating to any anything within the class so it can be anything. Correct. Yes so it can be anything. I just asked that to make sure that everyone understands. I mean we really could call this robo if we wanted to. But I'll keep it new game. And then what's going to happen. Is we'll come back in here and let's run. So the equals sign it's so game is referring to the class. What about new so yeah new game or .new Also it's an available. Ruby method predefined Ruby method that just lets us create a new instance of a class game.

So if. Everyone has lets go ahead and I can kind of show you something neat that will make a little bit more sense with that. So if you open up your portfolio. Well we got a lot of stuff open. But we're using. The new .new method all the time to create a new instance of the class blog. Oh new blog posts?. Correct. So what this is actually doing because we do have the method right here that saying create and that's allowing us to create the blog post and everything but this blog.new what its literally doing is it's creating a new instance. Of the class that has blog. So here we have class blog.

So you can see. Coming back to my blogs controller this right here that @blog.new looks very similar to my game.new OK. Well. You Yeah it's the first time I've really done it. Yeah. And I mean it's really neat because when you think about it rails really is just a massive jam for Ruby itself. So rails is using basic and also more and more advanced but it's all just built out of Ruby and Ruby logic. So that's one thing. As you start coding you'll be like

Oh okay that makes a lot more sense. That's why I called the .new inside of rails. And the greeting is referring to the method in the game correct. So now this is doing is it's going to do is instantiate the class game or create a new instance of it because initialize will run beforehand. All we have to do is call this next method. So when I run it right here Ill say "Hello James meet your rival Tyson"so is that making more sense to everyone.

Yeah it definitely makes sense. perfect. So before we move on let's go ahead and I'll show you a couple other ways that we can kind of use the method of initialized so one way that you can do is by passing in an argument so we'll pass on the argument of name and rival. And then inside the method what we'll do is we'll say grab the argument of name and set that to. The instance variable of name and same thing for rival. Now if we run it just as it is here you'll see that we're going to get an error. And Ruby tells us exactly what we need.

So initialize wrong number of arguments given. Are Wrong number of arguments given zero and expected two. So all we have to do is when we call the new instance of game we just have to pass those variables so we can pass and James again and then we'll pass in Tyson for the rival. So now with these past in it will accept it inside of a initialize. And then it'll become, these instance variables will be available to our method of greeting.

class Game
   def initialize(name, rival)
      @name = "James"
      @rival = "Tyson"
   end

   def greeting
      puts "Hello #{name}. meet your rival #{rival}"
   end
end

new_game = Game.new("James", "Tyson")
new_game.greeting

So we can run it again over here and you can see that we had the exact same. Output as we did when it was hardcoded but now in this case instead of it constantly being set to James we could call it. Michael. And Carl. And when you run it. You could also do something like this because you will. I mean the idea is that you can create multiple instances of. So if we call this new game one and new game one and go back to James and Tyson.

And save it you'll see that it gives us two different outputs so giving passing in our arguments just allows us for a lot more flexibility without having to hard code and necessarily start overwriting instance variables too early. And then a way that we're going to do it just to start this game out is I'm actually going to use. The gets.chomp method.

So what will first do. Is we're going to revert back to not passing in any arguments. And I'm just going to ask for put statement. And I'm kind of start to tell the story that we're going to go over today so. OK. So now for what we're going to do is instead of calling name will say gets.chomp And put the exclamation mark. What that will do it it will take our user input. Clean it up with. From all the stuff that Ruby had added to it and then it will set at name to whatever name the user inputs and we'll also do the same thing for rival.

class Game
   def initialize
      puts "You are about to embark on a new career. What is your name?"
      @name = gets.chomp!
      puts "What is your rivals name?"
      @rival = gets.chomp!
   end

   def greeting
      puts "Hello #{name}. meet your rival #{rival}"
   end
end

new_game = Game.new
new_game.greeting

Is there anyone that's not familiar with that gets.chomp or what. gets.chomp does. Great. So let's go ahead and now see what happens when we run this set as it is set up right now. So as I mentioned before even though we're calling the .greeting the initialize will always run first. So that's now giving us as puts statement there asking us what our name is. So just say James and my rival's name is Tyson. So hello James meet your rival. Tyson so again we're getting the same output every time. So that's just the way that I've kind of chosen to set it up so far.

But now what I want to do is start creating additional classes so we can kind of see how it works. How classes work and interact with one another. So what we're going to do now is. Going to create a class of Career. So here what I want to do is just allow the users to pick their career path. What we're going to do is eventually allow or what I want to build out for the game is allow a user or a player to come in and choose whether to take the. Developer path or the fast food worker. So with this creative. Lets just quickly create. A method. That will add to this in a little bit but I just won't break it down into small simple pieces. First.

class Career
   def pick_your_career
      puts "You have started a new career"
   end
end

Okay so now what we have to do is these are two separate classes. If we wanted Career in Game to be able to communicate with each other. Then what we have to do is we can. Create inheritance and inheritance with Ruby is actually super easy. So eventually as we start to build this game out we're going to pass and create some career. Some variables inside a career that we want accessible to game.

class Game < Career

So what we do to allow that is we're just going to make sure that sorry we're going to make sure the game inherits from. Career. And I'm actually further down the line I'm actually going to. Refactor this a little bit more to make it cleaner and we'll use the inheritance in a little different way just kind of wanted to initially show how we can use it. So the really neat thing now is because we're using inheritance. Instead of calling creating dot here or greeting right here I can actually say pick your career right here. So this method is now available and the instance of game.

class Career
   def pick_your_career
      puts "You have started a new career"
   end
end

new_game = Game.new
new_game.pick_your_career

So as we run it here what we'll do instead. Let's see. When you take a. So. Oh I know. Exactly. OK. So what is happening is it's not finding career first. So I actually have to MU career. Above game which is inheriting from her. So now if we run it it will be able to. Run through the rest so James and we'll just call this tie for now. You started your career. So that was a simple mistake. And it's easy to make those.

But. In the case of inheritance the application first needed to have the classic career before it could continue. so that was where that small bug was. So moving on. So let's go ahead and build it out a little bit more so I'm actually going to take in this to save me from all the typing. I'm going to just copy and paste the code and to pick their career. Well see. So. Right here what I've done is I've used a while loop and this just allows for it there's improper user input.

class Career
   def pick_your_career
      event= ""
      puts "\nTime to choose a new career !\nEnter 'd' to become a developer, and 'f' to work in fast food.\nWhich career path would you like?"
      print event
      while choice
         when 'd'
            @salary = 55000
            @job = "Developer"
               break
          when 'f'
            @salary = 22000
            @job = "Fast Food Employee"
               break
           else
              puts "Please enter a 'd' or 'f'"
              print event
          end
        end
      end
end

So let's go ahead and I'll walk through that example. So you're about to embark on a new career. What is your name. James will say hi. K time to choose a new career. Entered D to become a developer and F to work in fast food. Which career path would you like to go. So there's ways that you can accomplish the same thing as the while loop. In and if that or if else statement I use while loop just because it's more simple to me.

But what this while loop is going to do is to put something that's not available like L. It will remind me please enter a d or a f so if I put in a longer stream so until I do D will it continue. So does that make sense why I chose to use a while loop here. Does anyone have questions about that. So the while loop allows it so that. You can only put in either d or f after you've been anything else will just directly loop back correct. How do you how can I decide. Which which which class to expand on in terms of putting methods within. I'm sorry. Oh no. Go ahead. Oh no. Like why not. I mean why not put it inside the. Game. It Game class.

Yeah. And it will vary from application to the application. How I'm building this now is there's definitely better ways and cleaner ways to build this. For example putting everything in game or putting more everything in career. How I just started this out was creed. I wanted to be able to show you inheritance right off the top especially if you don't have time to get towards the end of the application but it's really just going to vary from application to application and. Kind of kind of each situation. But for example in rails it's very it's very specific so you know log right here is.

Inheriting from application record which is also a class the class of application record is inheriting from active record based which is a ruby method so it really just depends on how you're planning on building out your final application. For the most part it should be pretty straightforward. Okay you know you're right. My pick your career should be inside the game or so worth it you'll see as we get a little further into the refactoring towards the end.

Kind of how I've decided to do it because you are on the right track. Thinking about that Michael. And refactoring is rearranging correct time and cleaning things up. I'm making sure it's clean easy to read code. So I've definitely learned something new that like in order to have an inheritance. That has to be up above. Right. Whatever you're saying. Yeah and it threw me through a loop. At first when I was like OK what's what's breaking right here.

So I mean there's things like that all the time that will run into. Okay so now with this how I've kind of decided to build this out a little further is I'm going to create another a couple of other classes. And those are going to be for the different paths that a user can take. So for example we have the class of Developer. And and the class of FastFood. I want to make sure I'm not skipping anything. So how I've chosen to kind of build this out.

Right here is and now is where I'd like to use the. Sorry passing in the argument. So what will go ahead and do is will create a new method right here the initialize method. And I'm immediately just going to pass in a (name, rival, job, salary) which you can see I'm going to we're going to find the salary and job right here. And we already decided the name and arrival here. Oh sorry. Oh no go ahead.

The job it will connect to the class career and the salary they will connect to the class. Correct. Because the instance variable up there. Got it right. And because currently game is inheriting career that makes the job and salary available to game as well. Without this. Inheritance. These instance variables won't be available because they're only available inside of that class as instance variables, but adding this inheritance that makes them available. K so I'm going to go ahead and. Again just delete.

So I'm deleting the greeting inside of game and then I pre built a little bit of code here that I'm going to put inside. Of developer and fast food. And then. Because we're initializing it with variables or sorry arguments that are being passed all we have to do now to reset up the variables is just pass it and like I showed you near the beginning. I'm going to copy and paste this into fast food.

So now as you can see where we've created the class Developer in the class FastFood but they're not inheriting from anywhere but we are passing in these variables so what we can do is I'm going to create inside the game a new method. And I'll call it a career_path. And I'll just use a quick if else statement. So if @job is equal to Developer. Then we will run a new instance of the class Developer so we'll call this developer_path.

Developer.new and this time we just have to pass in those variables so what it's going to allow us to do is still use those variables as long as we give it the @ sign properly calling. So we'll just do this. (@name, @rival, @job, @salary). So quickly add an end. Before I forget the else. Oh and I forgot to add. So now we will say developer_path.greeting. So I redefined the greeting method right here inside a developer. Now we can quickly just copy this and say else we'll call this fast_food_path.greeting and then call it FastFood OK. So can anyone tell me what they think is going to happen now.

Is it going to give like give me a choice. And if I choose d that it's going to pass me through the questions for d or through that information for for d and for f the information for f. Yes. Yes. So what it's going to do. And I actually forgot to put in at the end of your career right after it runs through the while statement. I actually need to call in the method of career_path. So we'll set that and know what it's going to do is same thing it's done before it'll ask us for name arrival and then whether we want to be a developer or fast food and then when it runs through developer I'm telling it to rank the career_path method.

It'll come down find a method career_path and it'll run that. So if job is equal to developer then developer_path equals Developer.new and it's passing in each of these variables. Cool yeah. So it is really neat and it really creates out flexibility and as to kind of. You know explore the different. The different possibilities within the different classes and how it works. But now personally as because I mean I built this when I first started building out this application I decide how it was going to work.

I didn't plan on doing it that way. So you can see right here we had a lot of duplicate code so developer and fast food. It's the exact same code every single time. So what I how I ended up wanting to refactor it. Is kind of talking a little bit more. About what Michael asked earlier so what we're going to go ahead and do is I'm actually going to say take career out or I'm going to take out an inheritance.

Off of game and I'm just going to call career to be the main one. So career is going to be the main initial set up of every thing so we'll run the initialize method. And the career_path method. Inside of the class of Career right here. So now with that taking care of what what I have to do is I to no longer called name. So I'll change the game to career. And then I'm going to pass everything inside of my developer class into game. And remove it from FastFood.

And then I will actually tell developer to inherit from game as well as fast food. So now what will happen is it's a lot cleaner so let's just run through this really quickly so as you run through It will ask for name again, it'll ask the rival's name. So it's running the exact same way it was running before but it's a little bit cleaned up. So now career is a whole setup of the game and then Game is initialized by passing in. This information and we can call developer still and fast food still because their methods are available with in game which is being inherited by developer and fast food. So that's why you can have classes without anything in it because it has an inheritance which connects back to the original, correct.

So just like we were I was briefly showing inside of our portfolio application when you're first starting out blog will be completely empty but it's inheriting from application record which is inheriting from active record. So it's all being set up in rails automatically for us which makes rails super easy and super nice. So inheritance is kind of like the bridge for like websites. It's like a city. Exactly. So we're about out of time but we can wrap a few things up so let's just say. getting_started. puts "Welcome to your first developer job". And then lets call this first_job.

And I'm just calling these different methods so you can kind of see what's going to happen but we'll puts "Welcome to your first day on the job". Got to remember to end it. And if. So now if we want to just skip creating we can in the developer path we can say OK let's run getting started and in the fast food path we can run fast_food_path.first_job so I'll clear out my terminal and then we'll run it again. So it will ask us our name and rivals name and it also gets to pick a path and welcome to your first development job.

So one more thing I'm going to do just so you can see it's kind of available is I've installed pry inside my application. So will require 'pry' up here and pry is a great tool for debugging and kind of learning what's available and. kind of what the bugs are within your application. But for this what we're going to do is I'll require pry at the top and then inside a developer what I'll do is a method of getting started.

I would call or require or sorry binding.pry and we'll run it again. K and what you can see is pry allowed us to stop everything and this will be another class to really get into this but I want you guys to see kind of what was available so as we call pry we can call the variable of name and it will tell me that nothing is available. Let's try this again. so job is available. Salary available so something happened when I was putting in my name. That didn't allow it to properly pass through correctly.

This looks a lot like the rails console. Its cause I call these @names. So there we go. So now if I went back and printed I mean I could even call. I know it will be. Yeah I'll still be no but. So what happened there is I had put name instead of names so it didn't properly pass through correctly because names didn't have any set. Variable or name wasn't set to equal anything.

So that's why it broke and gave us a kneel right here. But as you can see when we pass all these arguments through into the initial into initialized game and then then set the inheritance the rest of the variables all pass through correctly so that's just kind of how we were able to have those all those different classes speak to one another. And then you can see the end. It was. Well now if I type exit to get out of pry it I'll say welcome to your first adult job so we can now set up all the arguments with inside developer to continue the game or with fast food. Is there any other questions or anything that anyone has.

This is this has been significantly helpful. You know I'm really glad so yeah so I will go ahead and I'll make this code available in this video available to everyone. Just in case you kind of want to build out the game or kind of deep dive a little bit more but I just wanted to really get through how the classes are working with one another. One other really neat thing that I found all kind of setting up for this class if I can find it.

Is this right here. So let's. Go back to learning classes. And we will comment out everything right here. So let's say we have a class of Animal. And. You called @dog. Let's see. Lets do def. Hey I've got to make sure that arguments are available. So I was really curious because if we called let's just say Animal.methods and then let's have it print them or puts it using a put's statement and then let's show the terminal to display it.

So as I was going through all this I really wanted to know if kinda of which methods would be available that were very similar to Rails because I wanted to really create a clear parallel. So what we can do. Let's run the ruby. Learning classes so it's printed all of these. But I wanted to kind of understand how it's like in this application if we came in to the rails console and. I could call it it's going a little slow. So we could call blog.all and it shows us an instance of all the different blogs so there right now. So let's go back and let's say. my_pets = Animal.new("Pepper", "Molly", "Tweety", "gumbo")

And then we call. my_other_pets = Animal.new("Jerry", "Sally", "Silvestor", "Doug"). So if I wanted to call Animal.all you can see when I called the dot method earlier all is not an available method like it is in rails so I kind of wanted to learn how to do that a little bit so what I discovered in my research and I'll comment this out really quickly. Just so we can kind of see as I discovered this really neat class called object space.

So what we can do is call a class just like that earlier. At the beginning of the Webinar. When you run. . There we go. So it had just taken a while to save so it ran it as you can see that ObjectSpace is a class type of module. So what this object space creates some really neat features and kind of where are you. It allows you to further explore and expand what you're doing within your application.

So let me go ahead and jump back in to what we will be creating. OK. Sorry I just want to find the code because I haven't memorized it yet but it's pretty neat what you can do. OK. So what we'll do is we'll use this object space. And inside of the class of animal will create a new method called self.allocate. Objects based on each object cell to rate. So now is as we've a lot of you have covered insider rails when you're creating a custom scope. This stuff itself makes us able to call.

The name of the class and then all and make it accessible somehow. We go ahead and we'll run my pets and my other pets as instances of the class of animal and then we'll just call animal.all and I'll set a puts statement right here so we'll print it. Now let's see. So I think it's correct. Very good. So you can see here that we got the IDs of each instance of class. And so as you further break this down you can set it up in other ways to make it.

So these are available and so forth But I thought it was just a really neat way to. Kind of break it down further. So you're seeing kind of what's happening with in rails. So that just a little fun thing that I had I'd close with but are there any other questions. Before we end for tonight. So. OK well thank you so much for your participation. This Thursday we will have Ben with us and Ben will actually be covering how to use git hub. Git hub is a tool that we're all familiar with the basics as we've kind of got started and dove. Into things but he's going to be covering certain instances like rebasing and stuff and how you're really going to end up using.

Git hub beyond just personally but also for going back on any errors that you may have created on a branch and also rebasing so developing to one get hub in a team environment. So it'll be a really neat class and I hope that everyone's there and everyone can make it and we'll start that around 530 Mountain time. But you guys have a great night. And if there are any questions feel free to reach out to either Ben, myself or one of the T.A's on tonight or any of the mentors are available just any time throughout the day but you guys have a great night.