ruby practice
35039 ワード
class Ambiguous def x 1 end def test puts x x = 0 if false puts x x = 2 puts x end end am = Ambiguous.new am.test
#! /usr/bin/ruby
#
# “catch(:done)” ,
# “throw :done” , “catch(:done)” ,
# 。 catch/throw , throw ,
#Ruby catch,
# , ,catch 。
#Ruby goto , catch/throw goto 。
#
def
m
(
n
)
puts
n
throw
:done
unless
n
>
0
m
(
n
-
1
)
end
catch
(
:done
)
{
m
(
3
)
puts
"Cannot reach here!"
}
puts
"Reach here!"
class
FixedStack
<
Stack
def
initialize
(
limit
)
super
()
@limit
=
limit
end
def
push
(
val
)
if
@sp
>=
@limit
puts
"Over limit"
return
end
super
(
val
)
end
def
top
return
@stack
[-
1
]
end
end
#!/usr/bin/ruby
require
"observer"
require
"tk"
class
WatchModel
include
Observable
def
initialize
@running
=
false
@time
=
0
@last
=
0
.
0
Thread
.
start
do
loop
do
sleep
0
.
01
if
@running
now
=
Time
.
now
.
to_f
@time
+=
now
-
@last
puts
@time
@last
=
now
changed
notify_observers
(
@time
)
end
end
end
end
def
start_stop
@last
=
Time
.
now
.
to_f
@running
=
!
@running
end
def
time
@time
end
end
class
WatchWindow
def
initialize
model
=
WatchModel
.
new
model
.
add_observer
(
self
)
@label
=
TkLabel
.
new
(
nil
)
.
pack
(
'fill'
=>
'x'
)
self
.
update
(
0
)
btn
=
TkButton
.
new
btn
.
text
(
"start/stop"
)
btn
.
command
(
proc
{
model
.
start_stop
})
btn
.
pack
(
'fill'
=>
'x'
)
btn
=
TkButton
.
new
btn
.
text
(
'quit'
)
btn
.
command
(
proc
{
exit
})
btn
.
pack
(
'fill'
=>
'x'
)
Tk
.
mainloop
end
def
update
(
time
)
@label
.
text
format
(
"%02d:%02d"
,
time
.
to_i
,
(
time
-
time
.
to_i
)
*
100
)
end
end
WatchWindow
.
new
require "observer"
class Tick include Observable def tick loop do now = Time.now if now.sec[0] != 1 changed end notify_observers(now.hour, now.min, now.sec) sleep 1.0 - Time.now.usec/1000000.0 end end end class TextClock def my_update(h, m, s) printf "\e[8D%02d:%02d:%02d", h, m, s stdOUT.flush end end if $0 == __FILE__ tick = Tick.new tick.add_observer(TextClock.new, :my_update) tick.tick end
#! /usr/bin/ruby
def add_gram_to_multi_word(token) pron = token.split("-").collect {| w | w.chomp.strip}.join("") stories = Dir.glob('stories/*') stories.each do |story| story_changed = false File.open story do |file| arr = file.collect do |line| if line =~/
require
'singleton'
class
PrintSpooler
include
Singleton
end
class
Range
def
by
(
step
)
x
=
self
.
begin
if
exclude_end?
while
x
<
self
.
end
yield
x
x
+=
step
end
else
while
x
<=
self
.
end
yield
x
x
+=
step
end
end
end
end
module Seq
def self.fromtoby(from, to, by) x = from while x <= to yield x x += by end end end if __FILE__ == $0 Seq.fromtoby(1, 10, 2) {|x| puts x} end
class
Stack
def
initialize
@stack
=
[]
@sp
=
0
end
def
push
(
value
)
@stack
[
@sp
]
=
value
@sp
+=
1
end
def
pop
return
nil
if
@sp
==
0
@sp
-=
1
return
@stack
.
pop
end
def
to_s
@stack
end
end
require "builder/xmlmarkup"
xml = Builder::XmlMarkup.new(:indent => 2) puts xml.html { xml.head { xml.title("History") } xml.body { xml.h1("Header") xml.p { xml.text!("paragraph with") xml.a("a Link", "href"=> "http://onestepback.org") } } }
class
Sequence
def
initialize
(
from
,
to
,
by
)
@from
,
@to
,
@by
=
from
,
to
,
by
end
def
each
x
=
@from
while
x
<=
@to
yield
x
x
=
x
+
@by
end
end
def
length
return
0
if
@to
<
@from
Integer
((
@to
-
@from
)
/
@by
)
+
1
end
def
[]
(
index
)
return
nil
if
index
<
0
v
=
@from
+
index
*
@by
if
v
<=
@to
v
else
nil
end
end
def
*
(
factor
)
Sequence
.
new
(
@from
*
factor
,
@to
*
factor
,
@by
*
factor
)
end
def
+
(
offset
)
Sequence
.
new
(
@from
+
offset
,
@by
+
offset
,
@by
)
end
end
if
__FILE__
==
$0
s
=
Sequence
.
new
(
1
,
10
,
2
)
s
.
each
{
|
x
|
puts
x
}
end