-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby-classes.rb
158 lines (136 loc) · 3.71 KB
/
ruby-classes.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Ruby Classes and Objects 10/3/2022
# Classes and methods can be predefined by the langauge
# Keyword class and keyword end define the scope of our class
# PascalCase - the naming convention for classes, class names are typically singular
# class Hulu
# end
# Classes will not do anything on their own
# Classes must be instantiated
# p Hulu.new
#<Hulu:0x000000015b927ed8>
# Unique instance of my class
# Need to be able to define data and to update that data: getter and setters
# Setter method that takes in information and assigns it to a variable that belongs to the class, in JavaScript - this, in Ruby - @ instance variable
# class Hulu
# def set_show_data(title, run_time)
# @title = title
# @run_time = run_time
# end
# end
#
# severance = Hulu.new
# p severance.set_show_data
# Error: wrong number of arguments (given 0, expected 1)
# p severance.set_show_data('Severance')
#<Hulu:0x000000012f05f1d8 @title="Severance">
# p severance
# p severance.set_show_data('Severance', '40min')
# p severance
#<Hulu:0x0000000152852cc8 @title="Severance", @run_time="40min">
# Getter methods:
# class Hulu
#
# def set_show_data(title, run_time)
# @title = title
# @run_time = run_time
# end
#
# def get_title
# @title
# end
#
# def get_show_data
# "The show #{@title} is #{@run_time} long."
# end
#
# end
# severance = Hulu.new
# severance.set_show_data('Severance', '40min')
# p severance.title <-- undefined method `title'
# p severance.get_title <--"Severance"
# p severance.get_show_data
# => "The show Severance is 40min long."
# p severance
# Initialize is a method that is called by .new and will act as a setter method to instatiate data
# class Hulu
# # refactor our setter to be initialize
# def initialize(title, run_time)
# @title = title
# @run_time = run_time
# end
#
# def get_title
# @title
# end
#
# def get_show_data
# "The show #{@title} is #{@run_time} long."
# end
#
# end
# # severance = Hulu.new
# # => (given 0, expected 2)
# severance = Hulu.new('Severance', '40min')
# p severance
# Setter methods that perform logic
# class Hulu
# # setter method:
# def initialize(title, run_time)
# @title = title
# @run_time = run_time
# @watch = false
# end
#
# # getter method:
# def get_title
# @title
# end
#
# # getter method:
# def get_show_data
# if @watch
# "You have watched the show #{@title} is #{@run_time} long."
# else
# "You have not watched the show #{@title} is #{@run_time} long."
# end
# end
#
# # setter method:
# def been_watched
# @watch = true
# end
# end
#
# severance = Hulu.new('Severance', '40min')
# #<Hulu:0x000000013e0e5968 @title="Severance", @run_time="40min", @watch=false>
# p severance.get_show_data
# severance.been_watched
# p severance.get_show_data
# Ruby helper methods - methods that create more methods
# attr_accessor - takes a symbol of an instance variable, when you see a symbol think: "the thing called..."
class Hulu
# attr_accessor creates a getter and a setter method for the instance variables it gets passed
attr_accessor :title, :run_time, :watch
# setter method:
def initialize(title, run_time)
@title = title
@run_time = run_time
@watch = false
end
# getter method:
def get_show_data
if @watch
"You have watched the show #{@title} is #{@run_time} long."
else
"You have not watched the show #{@title} is #{@run_time} long."
end
end
end
severance = Hulu.new('Severance', '40min')
#<Hulu:0x000000013e0e5968 @title="Severance", @run_time="40min", @watch=false>
p severance.get_show_data
severance.watch = true
p severance.get_show_data
p severance.title
severance.title = 'Severance Two Electric Boogaloo'
p severance.title