-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuby
More file actions
162 lines (160 loc) · 3.76 KB
/
Ruby
File metadata and controls
162 lines (160 loc) · 3.76 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
"Veronika"
=> "Veronika"
Success!
> "Veronika".reverse
=> "akinoreV"
Success!
> "Veronika".length
=> 8
Success!
> "Veronika" *5
=> "VeronikaVeronikaVeronikaVeronikaVeronika"
Success!
> next
> 40.reverse
=> #<NoMethodError: undefined method `reverse' for 40:Fixnum>
Oh no!
> 40.to_s.reverse
=> "04"
That's better!
> []
=> []
Success!
> [12, 47 ,35]
=> [12, 47, 35]
Success!
> [12, 47, 35].max
=> 47
Success!
> ticket= [12, 47, 35]
=> [12, 47, 35]
Success!
> ticket
=> [12, 47, 35]
Success!
> ticket.sort
=> [12, 35, 47]
> next
> print poem
=> "My toast has flown from my hand
And my toast has gone to the moon.
But when I saw it on television,
Planting our flag on Halley's comet,
More still did I want to eat it."
Success!
> poem['toast'] = 'honeydew'
=> "honeydew"
Success!
> print poem
=> "My honeydew has flown from my hand
And my toast has gone to the moon.
But when I saw it on television,
Planting our flag on Halley's comet,
More still did I want to eat it."
> next
> poem.reverse
=> ".ti tae ot tnaw I did llits eroM
,temoc s'yellaH no galf ruo gnitnalP
,noisivelet no ti was I nehw tuB
.noom eht ot enog sah tsaot ym dnA
dnah ym morf nwolf sah wedyenoh yM"
Success!
> poem.lines.to_a.reverse
=> ["More still did I want to eat it.
", "Planting our flag on Halley's comet,
", "But when I saw it on television,
", "And my toast has gone to the moon.
", "My honeydew has flown from my hand
"]
Success!
> print poem.lines.to_a.reverse.join
More still did I want to eat it.
Planting our flag on Halley's comet,
But when I saw it on television,
And my toast has gone to the moon.
My honeydew has flown from my hand
Success!
> next
> books={}
=> {}
Success!
> books ["Gravity's Rainbow"] = splendid
=>
#<Racc::ParseError: (string):1 :: parse error on value "=" (tEQL)>
> books ["Gravity's Rainbow"] = : splendid
=>
#<Racc::ParseError: (string):1 :: parse error on value "=" (tEQL)>
> books["Gravity's Rainbow"] =:splendid
=> :splendid
Success!
> books.length
=> 1
> books["Gravity's Rainbow"]
=> :splendid
Success!
> books.keys
=> ["Gravity's Rainbow"]
> next
> ratings = Hash.new
=> {}
Success!
> books.values.each { |rate| ratings [rate] +=1}
=> #<NoMethodError: undefined method `+' for nil:NilClass>
> books.values.each {|rate| ratings [rate] += 1 }
=> #<NoMethodError: undefined method `+' for nil:NilClass>
> ratings
=> {}
> next
> 5.times {print "Odelay!" }
=> "Odelay!Odelay!Odelay!Odelay!Odelay!"
Success!
> next
> Dir.entries "/"
=> [".", "..", "Home", "Libraries", "MouseHole", "Programs", "Tutorials", "comics.txt"]
Success!
> Dir ["/*.txt"]
=>
#<NoMethodError: undefined method `Dir' for main:Object>
> Dir["/*.txt"]
=> ["/comics.txt"]
Success!
> print File.read ("/comics.txt")
=> "Achewood: http://achewood.com/
Dinosaur Comics: http://qwantz.com/
Perry Bible Fellowship: http://cheston.com/pbf/archive.html
Get Your War On: http://mnftiu.cc/
"
Success!
> FileUtils.cp('/comics.txt', '/Home/comics.txt')
=> nil
Success!
> Dir["/Home/*.txt"]
=> ["/Home/comics.txt"]
> next
> File.open("/Home/comics.txt", "a") do |f|
.. f << "Cat and Girl: http://catandgirl.com/"
.. end
=> #<File:/Home/comics.txt (closed)>
> print File.read ("/Home/comocs.txt")
> print File.read ("/Home/comics.txt")
=> "Achewood: http://achewood.com/
Dinosaur Comics: http://qwantz.com/
Perry Bible Fellowship: http://cheston.com/pbf/archive.html
Get Your War On: http://mnftiu.cc/
Cat and Girl: http://catandgirl.com/"
> next
> File.mtime("/Home/comics.txt")
=> 2015-10-18 00:45:38 UTC
Success!
> File.mtime("/Home/comics.txt").hour
=> 0
Success!
> next
> def load_comics (path)
.. comics = {}
File.foreach(path) do |line|
name, url = line.split(': ')
comics[name] = url.strip
end
comics
end