-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubyway.rb
324 lines (202 loc) · 4.37 KB
/
rubyway.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#comment
=begin
comment
=end
a=3
b=79
p "#{a} #{b} #{a*b}" #"3 79 237"
p arr = [1,2,3,4,5] #[1, 2, 3, 4, 5]
p arr1 = [1,2,3,4, "alpha"] #[1, 2, 3, 4, "alpha"]
p arr2 = [1,2,[3,4],5] #[1, 2, [3, 4], 5]
p arr3 = ["alpha", "beta", "gamma", "delta"] #["alpha", "beta", "gamma", "delta"]
p arr[1] #2
p arr1[4] #"alpha"
p arr3[3] #"delta"
p %w[alpha beta gamma delta] #["alpha", "beta", "gamma", "delta"]
p %w(Jan Feb Mar Apr Иау Jun Jul Aug Sep Oct Nov Dec) #["Jan", "Feb", "Mar", "Apr", "Иау", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
hash = {1=>2, 3=>4, 5=>8, 2=>12}
p hash #{1=>2, 3=>4, 5=>8, 2=>12}
hash2 = {"cat" => "cats", "ox" => "oxen", "bacterium" => "bacteria"}
p hash2 #{"cat"=>"cats", "ох"=>"oxen", "bacterium"=>"bacteria"}
p hash[5] #8
p hash2["ox"] #"oxen"
case "I love ruby"
when "Love book"
puts "1"
when "Love car"
puts "2"
when "I love ruby"
puts "3" #3
else
puts "4"
end
score = 10
case score
when 1..5
puts "You rank Base"
when 6..10
puts "You rank Moderator" #You rank Moderator
when 10..15
puts "You rank Admin"
else
puts "No rank"
end
book = %w{book1 book2 book3 book4}
i = 0
while i < book.size do
print "#{book[i]} " #book1 book2 book3 book4
i += 1
end
i = 0
until i == book.size do
print "#{book[i]} " #book1 book2 book3 book4
i += 1
end
for i in book do
print "#{i} " #book1 book2 book3 book4
end
book.each do |i|
print "#{i} " #book1 book2 book3 book4
end
i = 0
n=book.size-1
loop do
print "#{book[i] }" #book1 book2 book3 book4
i += 1
break if i > n
end
n=book.size-1
0.upto(n) do |i|
print "#{book[i] }" #book1 book2 book3 book4
end
book.each_index do |i|
print "#{book[i] } " #book1 book2 book3 book4
end
class Friend
def initialize name, sex, phone
@name, @sex, @phone = name, sex, phone
end
def hello
puts "#{@name} #{@sex} #{@phone}"
end
end
name = Friend.new("Den","F","555-55-55")
name2 = Friend.new("Aria","M","777-77-77")
name.hello #Den F 555-55-55
name2.hello #Aria M 777-77-77
puts "===================================================="
p s1 = "String"
p s2 = "Mr O\'Brien"
p s3 = "C:\\TEMP"
s4 = <<EOF
String
new String
new String
EOF
puts s4
s5 = " String\n new String\n new String\n"
puts s5
p s5.size
p s5.length
p s5.count("a-z")
s5.each_line.with_index do |i, num|
print "Num #{num + 1}: #{i}"
end
s6 = "test text"
p s6.downcase
p s6.upcase
p s6.capitalize
s7 = "I love Ruby"
p s7.strip[7,4]
p s7.strip[2,4]
p s7.strip[0..5]
p s7.strip[7,4] = "PHP"
s8 = " <h1> text, spam, hi, bro, spam! "
p s8.sub(/spam/, "***")
p s8.gsub(/spam/, "***")
p s8.index("spam")
p s8.rindex("spam")
p s8.include?("spam")
p s8.scan("spam")
p s8 << "new string\n"
p s8.chomp
p s8.strip
p s8.reverse
p s8.delete(",!")
p s8.delete("<h1>")
=begin
require 'digest'
p Digest::SHA256.hexdigest("password")
puts "Enter you password"
password = gets.chomp
p Digest::SHA256.hexdigest(password)
=end
puts "===================================================="
p 64**2
p 9**3
p 2**4
p 3/3
p 3/4
p 9/3
p 3.0/4
p 3.0/4.0
p 3.49.round
p 4.50.round
p Time.new
p Time.mktime(2016)
p Time.mktime(2016,6,2,17,05)
p Time.local(2016,6,2,17,05)
var = ["apple","car","cat","phone","pc","gamebox"]
p var.values_at(0,3,4)
p var.values_at(0..2,5)
file1 = File.new("test1", "w")
file1.close
file2 = File.new("test2", "a+")
file2.puts "Add text"
file2.close
file2 = File.new("test2")
size = file2.size
file2.each do |i|
puts i
end
puts "Size file: #{size}"
file2.close
=begin
require "sqlite3"
# Open a database
db = SQLite3::Database.new "test.db"
# Create a table
rows = db.execute <<-SQL
create table numbers (
name varchar(30),
val int
);
SQL
# Execute a few inserts
{
"one" => 1,
"two" => 2,
}.each do |pair|
db.execute "insert into numbers values ( ?, ? )", pair
end
# Create another table with multiple columns
db.execute <<-SQL
create table students (
name varchar(50),
email varchar(50),
grade varchar(5),
blog varchar(50)
);
SQL
# Execute inserts with parameter markers
db.execute("INSERT INTO students (name, email, grade, blog)
VALUES (?, ?, ?, ?)", ["Jane", "[email protected]", "A", "http://blog.janedoe.com"])
# Find a few rows
db.execute( "select * from numbers" ) do |row|
p row
end
db.execute( "select name, email from students" ) do |row|
p row
end
db.close
=end