Girl Develop It is here to provide affordable programs for adult women interested in learning web and software development in a judgment-free environment.
Girl Develop It is dedicated to providing a harrasment free learning experience for everyone.
For more information, see our Code of Conduct.
How was last week's homework?
Do you have any questions or concepts that you'd like to discuss?
Expand upon the calculate method from class.
Make a class called "Calculator", and make each operation ("add", "subtract", "multiply","divide") into a method on Calculator
Run a demonstration of having all of the operations return a correct result!
What would you like to see reviewed?
Ruby has many classes predefined:
They are commonly used object types, and have methods associated with them already. How convenient!
a = Array.new
b = String.new
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.
Let's put together the last three classes and make a simple game!
This will cover things like Classes, Objects, Methods, and datatypes like Integers and Arrays
For the following examples, you should make folder called 'gdigame' and put all your files in there
What kind of game doesn't have Dice?
# die.rb
class Die
def roll!
@numberShowing = 1 + rand(6)
#rand(6) returns a random-ish number between 0-5
end
def showing
return @numberShowing
end
end
Inside the class, define its methods.
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.
Refactoring is taking existing code and moving or re-writing it to improve on the design or organization.
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.
# die.rb
class Die
def initialize
roll!
end
def roll!
@numberShowing = 1 + rand(6)
end
def showing
@numberShowing
end
end
# 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
# in irb
load 'character.rb'
me = Character.new("Cheri")
me.adventure!
me.heal!
me.adventure!
# repeat until you're done having fun
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.
Subclass: A class that inherits from another class. Ex: ScaryBook would be a subclass of Book
Superclass: A class that another class inherits from. Ex: Car would be the superclass of Sedan
# Elf is the Subclass
# Character is the Superclass
class Elf < Character
# ... more stuff goes here
end
Denote that Elf inherits from Character by using the <
symbol
# in character.rb, after Character class code
class Elf < Character
def twinkle
puts "I'm super magical!"
end
end
#in irb
load 'character.rb'
me = Elf.new("Cheri")
me.adventure!
me.twinkle
Notice that the initialize method was also inherited, as our new Elf knows its name and started off with 10 health.
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.
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 Dwarf < Character
def dig
puts "I'm a good digger!"
end
def heal!
@health += 4 # Dwarves don't heal as much as elves
end
end
#in irb
load 'character.rb'
me = Dwarf.new("Doug")
me.heal!
More information about inheritance can be found here.
For a command line program, you need these pieces:
require
s the class file(s)The file containing your class definitions, here called character.rb:
# character.rb
class Character
# contents of class here
end
class Elf < Character
# contents of class here
end
The file containing your program, here called adventure.rb, which will run from the command line and requires the class file:
# adventure.rb
require_relative 'character.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.....
Call the program file from the command line:
#in command line
ruby adventure.rb
# then have some fun!
Putting it all together: A role-playing game!
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? |
Practice: Make your final project even bigger and better! BONUS: Post it someplace so we can all play it!
Check out some of the other classes GDI Boston offers! https://www.girldevelopit.com/chapters/boston
@gdiboston
We are done! You are all offically programmers!
We have done a lot, I know you have questions so ask them!