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
135 lines (99 loc) · 3.54 KB
/
Copy pathclass_methods.rb
File metadata and controls
135 lines (99 loc) · 3.54 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
132
133
134
135
###################################
###### 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
## ATTRIBUTES (instance variables are often attributes of one of the objects - age, relationship status, etc.)
## INSTANCE METHODS
## behaviors of our class
#INITIALIZE METHOD
# 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}") #calling itself - making a new object of itself within itself A new object of pawn is going into array, pawns.
end
pawns
end
end
#make one pawn
one_pawn = Pawn.new("A2")
one_pawn.make_row("purple") # returns error - doesn't like that we called it on object of class (versus pulling on Pawn class itself, which works)
# #make a whole row of black pawns at the 7th row on chess board
# pawns = Pawn.make_row("black")
#
# #What is being stored in this local variable pawns? - stores all the pawns from else statement in array
# print pawns
#
# #WHAT IS THIS DOING - shuffles them and returns the position of first one from array
# puts pawns.shuffle.first.position
# pawns - array of pawn objects
# shuffle - array of pawn objects in different order
# first - returning first pawn object
# position - return position of pawn (instance method)
###################################
###### 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.