-
Notifications
You must be signed in to change notification settings - Fork 23
/
class_methods.rb
122 lines (91 loc) · 2.8 KB
/
class_methods.rb
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
###################################
###### 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
## INSTANCE METHODS
#INITIALIZE METHOD
## ATTRIBUTES
# 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.
###################################
###### 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
#
# #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.