-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuby_Syntax_Learning.arb
More file actions
170 lines (116 loc) · 2.86 KB
/
Copy pathRuby_Syntax_Learning.arb
File metadata and controls
170 lines (116 loc) · 2.86 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
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
#Initial Test document, learning basic Ruby usage, scripting
#Made while reading through https://www.theodinproject.com/paths/full-stack-ruby-on-rails/courses/ruby-programming
test1 = 2
puts test1.inspect
test1 **= 2
puts test1
print "Hello World"; print "I love drinking coffee\n"
puts "Hello World"; puts "I love drinking coffee"
#test2 = gets.chomp
#put test2
if test1 > 6
puts "test1 > 6"
else
puts "test1 <= 6"
end
test3 = 2
test3 += 2
puts test1.equal?test3 #pointer comparison, for numbers = true due to optimization
test1 = "hello"
test3 = "hello"
puts test1.equal?test3
puts 3 <=> 3 #"spaceship operator", returns -1, 0, 1 by larger value
response = 2 < 3 ? "2 < 3 = true" : " 2 < 3 = false"
puts response
i=0
#loop do
while i < 6 do #loop to 5
i+=1
end
puts "i = #{i}"
i=0
until i >= 4 do #Loop to 3
i+=1
#break if i == 5
end
puts "i = #{i}"
for j in ('a'..'e') # 1..5 #for 1-5
puts "for loop: j = #{j}"
end
#dictionary/map
hash = Hash.new
hash = {
#"a" => 1,
#"b" => 2
:a => 1,
:b => 2
#a: 1, ... ; still access with hash[:a]
}
hash[:c] = 500
hash.delete(:c)
puts hash.fetch(:b, -1)
puts hash.fetch(:c, -1)
#hash["b"]
#hash["no"] # = nil
#hash.fetch("no", "default value") # gives a keyerror w/o default
def greet(name)
"Hello, " + name + "!" #implicit returns
#return ^ also works
end
puts greet("Morgan")
def even_odd(number)
unless number.is_a? Numeric
return "not a number"
end
if number % 2 == 0
"even"
else
"odd"
end
end
puts even_odd("blarg")
# puts 5.even? # predicate methods end w/ "?"", return true/false
array = [5,3,2,5]
array.each do |num|
num **= 2
puts "squared array value = #{num}"
end
my_hash = Hash.new(100) #sets default value to 100
puts my_hash["cc"]
my_hash = { "aa" => 1, "bb" => 2}
puts my_hash["cc"]
my_hash.each { |key, value| puts "#{key} hash to #{value}" }
my_hash.each { |pair| puts "pair hash to #{pair} aka #{pair[1]} and #{pair[0]}" }
array = ["a", "b", "c", "d"]
array.each_with_index { |fruit, index| puts fruit if index.even?}
my_numbers = [5, 6, 7, 8]
#sum = 0
puts my_numbers.reduce(1000) { |sum, number| sum + number } # .reduce(1000) for 1026
#puts sum
puts array.select { |value| value < "c"}
puts "Testing objects"
class TestObject
@@total_indices = 0
def initialize(initial_values)
@values = initial_values.to_a
@@total_indices += initial_values.length
end
def push(value)
@values.push(value)
@@total_indices += 1
end
def get_index_value(index)
@values[index]
end
def get_total_indices()
@@total_indices
end
end
test1 = TestObject.new([1,2,3])
test2 = TestObject.new((5..12).to_a)
puts test2.get_total_indices()
puts test1.get_index_value(1)
test1.push(4)
puts test1.get_index_value(3)
puts test2.get_total_indices()
puts "end"