Just a little heads up:
Ruby returns the last evaluated line in a function/method by default so:
Code:
def my_x
13
return 14
12
end
my_x # => 14
def my_y
16
17
end
my_y # => 17
Here we go, lets start with the first:
Code:
class Die
end
def Die.roll
rand(6) + 1
end
Die.roll # => Class Method
Spoiler
Is the same as
Code:
class Die
def self.roll
rand(6) + 1
end
end
Die.roll # => Class Method
How?
when you use the
class keyword, you open the class for editing (therefore entering its
scope)
Within the classes scope,
self refers to the class object (Die in this case).
So:
Code:
module Die
p self # => Die
end
p Die # => Die
Now for the keyword
def:
Code:
def Die.roll
rand(6) + 1
end
module Die
def self.roll
rand(6) + 1
end
end
So essentially your saying: "define Die's roll function as"
And then we call the class function/method
Code:
Die.roll
die_module = Die # => Die
die_module.roll
Code:
class Die
def self.roll sides=6
rand(sides) + 1
end
# // Instance level
def initialize sides=6
@sides = sides
end
attr_reader :sides
def sides= n
@sides = n.to_i
end
def roll
rand(@sides) + 1
end
end
# // Create 3 new dies
dies = Array.new(3) do |i| Die.new i+3 end
p dies.collect &:roll # => [4,5,6] or something like that
# // Create 1 die, and fill the Array with 3 references to it
dies = Array.new(3,Die.new(7))
p dies.collect &:roll # => [7,7,7] assumming you where a lucky guy :3
Die.roll 7 # => 3
I actually made a few mistakes in the code
Anyway:
Classes and Modules are 1 of the same.
Code:
class Die
def self.roll(sides=6)
rand(sides) + 1
end
end
Die.roll 7 # => 5
In this case we have the
class method roll for
Die
That way we can make use of a roll function, without having to create an instance
of the class.
So what if we needed a portable dice or even multiple dies with different sides?
This is where the class comes in handy.
Code:
class Die
# // Instance level
def initialize sides=6
@sides = 6
end
attr_reader :sides
def sides= n
@sides = n.to_i
end
def roll
rand(@sides) + 1
end
end
die = Die.new 9
die.roll # => 6
die.roll # => 3
die.roll # => 8
die.roll # => 1
But lemme expand on that:
Code:
my_die_set = [Die.new(4),Die.new(6),Die.new(16)]
So here I have 3 different dies: 4 sided, 6 sided and 16 sided respectively
I can now move around with my dies and play D&D probably.
Anyway continuing, I believe you covered attributes, but here is a quick rundown:
Code:
# // The attr* trio
attr_reader :x
# // is the same as
def x
return @x
end
attr_writer :x
# // is the same as
def x= n
@x = n
end
attr_accessor :x
# // is the same as
attr_writer :x
attr_reader :x
continuing on: I shouldn't have used the Array as apart of the example
😉
Code:
# // Create 3 new dies
dies = Array.new(3) do |i| Die.new i+3 end
p dies.collect &:roll # => [4,5,6] or something like that
# // Create 1 die, and fill the Array with 3 references to it
dies = Array.new(3,Die.new(7))
p dies.collect &:roll # => [7,7,7] assumming you where a lucky guy :3
Die.roll 7 # => 3
Ignoring the comments:
Code:
dies = Array.new(3) do |i| Die.new i+3 end
p dies.collect &:roll
dies = Array.new(3,Die.new(7))
p dies.collect &:roll
Die.roll 7
Simplified:
Code:
dies = []
dies.push Die.new 1+3
dies.push Die.new 2+3
dies.push Die.new 3+3
results = dies.collect do |die| die.roll end
p results
dies = []
die = Die.new 7
dies.push die
dies.push die
dies.push die
results = dies.collect do |die| die.roll end
p results
Die.roll 7
Dont try to wrap your head around arrays yet, just remember, they are a collection
of objects in series.
push adds an object to the end of the array
unshift adds an object to the beginning of the array
pop removes and returns the last object in the array
shift removes and returns the first object in the array
collect is an
Enumerable method that iterates through the array and
collects the result as a new one
If your still not clear on something :3 I'll try to help.