-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture 1.rb
302 lines (108 loc) · 1.96 KB
/
lecture 1.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
2.to_s #this is to string method
2.methods #this shows all the methods
def foo(a)
return 5+a
end
##implicit returns
def foo(a)
a+5
end
good_name = 'helo' #naming convention
goodName #camel case... don't do this
class MyClass #use camel case
end
####
def hello(some_arg)
return 5
end
def hi(some_arg)
5
end
####
>>>hello "arg"
5
>>>hi "arg"
5
>>>hi hello "arg" # NONONONON
>>>hi(hello("arg")) #YES
#all lowercase true is the boolean not the True as in from python
###Strings
a = "Hello"
b = "World"
>>>puts a
Hello
=> nil
>>>puts "#{a} {b}"
Hello {b}
>>> puts "#{a} #{b}" #string interpolation
Hello World
=> nil
###this is better than python because in python we'd have to do
###print(a + ' ' + b)
>>>c = "Hello World"
>>>c[1..2]
"el" #prints 1 & 2
>>>c[1...2]
"e"
>>>c.upcase
>>>c.downcase
>>>puts true if "hello"
true
>>>puts true if 0
true
###everything in ruby is true except for nil and false. 0 is true!
###every if...else clause has a return value which is nil
>>> a = [1, 2, 3]
[1, 2, 3]
>>>a.first
1
>>>a.last
3
>>>a[0]
1
>>>a[1]
2
###hashes --- like python dictionaries
>>>h = { a: 1, b: 2}
>>>h[:a] #have to use :a and not a. :a is an immutable string.
1
>>>h[:b]
2
>>>h[:b] = 3
>>>:how
:how
>>>how
#Error. Undefine local variable or method 'how' for main
>>>a = :"helloworld"
>>>:helloworld == "helloworld"
false
#classes
class MyClass
def initialize(a)
@a = a #@ symbol has the instance variable
end
def self.hi #static method
puts 'hi'
end
def bye(b)
puts @a + b
end
end
>>>MyClass.new(6) #runs the initialize method automatically
#poetry mode
>>>m.bye 7
#this works too
def foo(a,b)
a+b
end
>>>foo 5, 6
11
>>>a = [1, 2, 3, 54]
[1, 2, 3, 54]
a.each do |i| #equivalent to for i in a
puts i+7
end
#at the end this still gives you the array a
(1..100).each do |i|
puts i
end