Intro to Ruby

Class 4

Welcome!

Girl Develop It is here to provide affordable programs for adult women interested in learning web and software development in a judgment-free environment.

Some "rules"

  • We are here for you!
  • Every question is important.
  • Help each other.
  • Have fun!

Girl Develop It is dedicated to providing a harrasment free learning experience for everyone.
For more information, see our Code of Conduct.

Review

  • Methods
  • Objects
  • Classes

Any questions?

Creating a Method Checklist from Learn Ruby the Hard Way

  • Did you start your method definition with def?
  • Does your method name have only characters and _ (underscore) characters?
  • Did you put an open parenthesis ( right after the method name?
  • Did you put your arguments after the parenthesis ( separated by commas?
  • Did you make each argument unique (meaning no duplicated names)?
  • Did you put a close parenthesis ) after the arguments?
  • Did you indent all lines of code you want in the method two spaces?
  • Did you end your method with end lined up with the def above?

Calling a Method Checklist from Learn Ruby the Hard Way

  • Did you call/use/run this method by typing its name?
  • Did you put the ( character after the name to run it?
  • Did you put the values you want into the parenthesis separated by commas?
  • Did you end the method call with a ) character?

Remember, to 'run,' 'call,' or 'use' a method all mean the same thing.

Built in classes of Ruby

Ruby has many classes predefined. We've talked about these examples:

  • String
  • Integer
  • Float
  • Array
  • Hash

They are commonly used object types, and have methods associated with them already.


              a = Array.new
              b = String.new

              

Built in classes of Ruby

When we create new objects from these classes,
we can do things with them immediately.

To see all methods associated with an object or class,
run .methods on it.


              b = "holy cow!"
              b.methods
              

You can view all the built in classes and their associated methods in the Ruby documentation.

Creating a Class

Let's practice creating your own classes in Ruby. Start by creating a new file die.rb.

  1. create a class Die
  2. create a method roll with an instance variable @numberShowing
  3. let @numberShowing equal a random number between 0-5
  4. create a method showing that returns @numberShowing


Note: Instance variables live in, and are visible everywhere in the object’s scope.

Using Die Class

You can use your class right away by loading it into IRB.


                #in irb
                load 'die.rb'
                die = Die.new
                die.roll
                puts die.showing
                

You can use it in another file by requiring it. We'll discuss this later.

This will be handy: initialize (source)

initialize is a special method with a special meaning in Ruby classes

Whenever you call the method new on a class, the class will create a new instance of itself.
Internally, the class will call the method initialize on the new object.

Let's add the initialize method to our Die class →

Refactoring a Class

What is the result of calling the showing method on a newly-created, un-rolled Die object? We can avoid this by calling the roll method as part of the creation of a new instance of Die.

Let's add the initialize method to Die

 
              # die.rb
              class Die

                def initialize
                  roll
                end

                def roll
                   @numberShowing = 1 + rand(6)
                end

                def showing
                  @numberShowing
                end

              end
              

Let's Develop It

  • In your text editor, create a "Character" class with an initialize method that sets the Character's name, and at least one other method.
  • Open IRB and load the class. Create one or more objects from the class and run some methods on them.
  • You could use the next slides as an example.

Example of creating a class


            # in character.rb
            class Character

              def initialize(name)
                @name = name
                @health = 10
              end

              def heal
                @health += 6
              end

              def adventure
                if @health > 0
                  puts "#{@name} goes on a great adventure and meets a dragon!"
                  puts "The dragon hugged #{@name} kind of hard..."
                  @health -= 5
                else
                  puts "#{@name} is dead :("
                  exit
                end
              end

            end
            

Running example program in IRB


                    # in irb
                    load 'character.rb'
                    me = Character.new("Cheri")
                    me.adventure
                    me.heal
                    me.adventure
                    # repeat until you're done having fun

                    

Inheritance

Classes can inherit from one other class.

All elves are characters, so if all characters have an adventure method, so do all elves.

Elves may also have their own methods that other characters do not.

Inheritance

Let's make an Elf!

  1. Start by creating a new file elf.rb
  2. create a class Elf that inherits from Character
  3. Add a method. Ideas?
  4. What happens if we add a heal method to Elf? Let's try!

Inheritance

Subclasses may differ from from their super classes in some ways. Methods can be overwritten when this is the case.


              # in character.rb, after Character class code
              class Elf < Character
                def twinkle
                  puts "I'm super magical!"
                end

                def heal
                  @health += 8 # it's easier to heal when you're magic
                end
              end

              #in irb
              load 'character.rb'
              me = Elf.new("Cheri")
              me.heal
              

More information about inheritance can be found here.

Let's create a command line program!

For your command line program, you need these pieces:

  1. File(s) containing your classes
  2. A file containing your program, which requires the class file(s)
  3. Call the program file from the command line (not IRB)

Set up the project folder

Create a folder gdi_game. Add the files containing your class definitions, character.rb, elf.rb, and any other characters you created.

Putting it all together

Create a file adventure.rb, which we can run from the command line to begin our game:


            # adventure.rb

            require_relative 'character.rb'
            require_relative 'elf.rb'

              #file path relative to the location of adventure.rb file
            require_relative 'die.rb'


            puts "Your name?"
            char = Character.new(gets.chomp)
            # plus lots more adventuring.....
            

Try it!

Call the program file from the command line:


              #in command line
              ruby adventure.rb
              # then have some fun!
                    

Let's Develop It

Putting it all together: A role-playing game!

  • If you didn't write a Character class, copy and paste the code to get one started.
  • Create a few subclasses of Character.
  • Write a command-line program that calls your character and die classes to allow the user to have an adventure!
  • Maybe user can choose their character type?
  • Maybe the user's progress will be dependent on outcome of dice-rolling?

Questions?

Further Resources

Ruby-doc.org Official Ruby Documentation
Project Euler Offers tons of small math problems to be solved via programming, plus forums to share and discuss solutions
Ruby Monk Free, interactive, in-browser tutorials
Ruby the Hard Way Fun, free (HTML version) book with great information about programming
Sinatra A basic web app framework that runs on Ruby
The Rails Tutorial The Rails web app framework is the most popular use of Ruby... are you ready to dive in?
More resources and files from class

Homework

Practice: Make your final project even bigger and better! BONUS: Post it someplace so we can all play it!

Intro to Programming in Ruby


We are done! You are all offically programmers!

Feel free to email me at kaileeagray@gmail.com

Contact Baron at bhines@fbsdata.com

Contact Brian at brian@brianpattison.com

Setup   |   Class 1   |   Class 2   |   Class 3   |   Class 4