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.
Any questions?
def
?end
lined up with the def
above?Remember, to 'run,' 'call,' or 'use' a method all mean the same thing.
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
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 practice creating your own classes in Ruby. Start by creating a new file die.rb
.
Die
roll
with an instance variable @numberShowing
@numberShowing
equal a random number between 0-5@numberShowing
Note: Instance variables live in, and are visible everywhere in the object’s scope.
Die
ClassYou 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.
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 →
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
# 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.
Let's make an Elf!
elf.rb
Elf
that inherits from Character
heal
method to Elf
? Let's try!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.
For your command line program, you need these pieces:
require
s the class file(s)Create a folder gdi_game
. Add the files containing your class definitions, character.rb
, elf.rb
, and any other characters you created.
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.....
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? |
More resources and files from class |
Practice: Make your final project even bigger and better! BONUS: Post it someplace so we can all play it!
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