forked from PilgrimMemoirs/class_methods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_methods.rb
More file actions
131 lines (96 loc) · 3.03 KB
/
Copy pathclass_methods.rb
File metadata and controls
131 lines (96 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
###################################
###### CLASS METHODS & SELF #####
###################################
#Currently we have been creating classes expect many object to be created from them to utilize their functionality.
#With the help of the keyword 'self', we will have
###################################
###### LEARNING GOALS #####
###################################
# Review what we've learned about classes so far:
#Constructor (initialize)
#Instance Methods
#Attributes (stored in instance variables)
# Discover new functionality within classes:
# Class Methods and
# self
###################################
###### CLASS_REVIEW #####
###################################
## INSTANCE VARIABLES
## Often Attributes
# ie name, username, password
## INSTANCE METHODS
##Behaviors of the class
# ie make a most, send a message,like a post, create a photo album
#INITIALIZE METHOD
## INITIALIZE METHOD
## Code run whenever a new class is created
# class Order
# def initialize(subtotal, quantity) # <= instance method
# @subtotal = subtotal
# @quantity = quantity
# end
#
# def total # <= instance method
# @subtotal * @quantity
# end
#
# def to_money # <= instance method
# # in the line below, `total` is invoking the instance method above
# "$" + sprintf("%0.02f", total / 100)
# end
# end
#
# order = Order.new(1000, 2)
# order.to_money #=> $20.00
###################################
######## CLASS METHODS #######
######## USING SELF #######
###################################
#Class methods are called directly by the class and not by an instance of the class.
class Kitten
def self.say_meow
return 'meow'
end
end
puts Kitten.say_meow
###################################
###### LETS TRY IT OUT! #####
###################################
class Pawn
attr_reader :position
def initialize(position)
@position = position
end
# This is the class method, it starts with self.
# It is only called on the class directly Pawn.make_row
def self.make_row(side)
if side == "white"
num = 2
else
num = 7
end
pawns = []
("a".."h").each do |letter|
pawns << self.new("#{letter}#{num}")
end
pawns
end
end
#make one pawn
one_pawn = Pawn.new("A2")
#make a whole row of pawns
pawns = Pawn.make_row("black")
#What is being stored in this local variable pawns?
print pawns
p Pawn.position
#WHAT IS THIS DOING!?
puts pawns.shuffle.first.position
###################################
###### WHERE IS THIS USED? #####
###################################
#Class methods are for anything that does not deal with an individual instance of a class
#In Gems, like faker
# https://github.com/stympy/faker/blob/master/lib/faker/hacker.rb
#When we get into databases, our data will be tied to a class. That class will have some premade class methods for us to use: .find, .last, .where
#Those class methods allow you to find specific objects of that class based on an specified attributes of that class.