summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
Diffstat (limited to 'sample')
-rw-r--r--sample/coverage.rb2
-rw-r--r--sample/drb/README.ja.rdoc2
-rw-r--r--sample/drb/README.rdoc4
-rw-r--r--sample/from.rb2
-rw-r--r--sample/getoptlong/abbrev.rb9
-rw-r--r--sample/getoptlong/aliases.rb8
-rw-r--r--sample/getoptlong/argv.rb12
-rw-r--r--sample/getoptlong/each.rb12
-rw-r--r--sample/getoptlong/fibonacci.rb62
-rw-r--r--sample/getoptlong/permute.rb12
-rw-r--r--sample/getoptlong/require_order.rb13
-rw-r--r--sample/getoptlong/return_in_order.rb13
-rw-r--r--sample/getoptlong/simple.rb7
-rw-r--r--sample/getoptlong/types.rb10
-rwxr-xr-xsample/mine.rb8
-rw-r--r--sample/mpart.rb44
-rw-r--r--sample/trick2015/kinaba/entry.rb4
-rw-r--r--sample/trick2018/02-mame/entry.rb4
-rw-r--r--sample/trick2022/01-tompng/Gemfile2
-rw-r--r--sample/trick2022/01-tompng/Gemfile.lock13
-rw-r--r--sample/trick2022/01-tompng/authors.markdown3
-rw-r--r--sample/trick2022/01-tompng/entry.rb40
-rw-r--r--sample/trick2022/01-tompng/remarks.markdown51
-rw-r--r--sample/trick2022/02-tompng/authors.markdown3
-rw-r--r--sample/trick2022/02-tompng/entry.rb32
-rw-r--r--sample/trick2022/02-tompng/remarks.markdown32
-rw-r--r--sample/trick2022/03-mame/authors.markdown3
-rw-r--r--sample/trick2022/03-mame/entry.rb27
-rw-r--r--sample/trick2022/03-mame/remarks.markdown96
-rw-r--r--sample/trick2022/03-mame/test.txt13
-rw-r--r--sample/trick2022/README.md14
-rw-r--r--sample/uumerge.rb2
32 files changed, 528 insertions, 31 deletions
diff --git a/sample/coverage.rb b/sample/coverage.rb
index 8e8d6167e2..42ba89fd50 100644
--- a/sample/coverage.rb
+++ b/sample/coverage.rb
@@ -49,7 +49,7 @@ at_exit do
end
end
- open(cfile, "w") do |out|
+ File.open(cfile, "w") do |out|
covs.zip(sources, pcovs).each_with_index do |(cov, line, pcov), idx|
cov += pcov || 0 if cov
cov = (cov ? (cov == 0 ? "#####" : cov.to_s) : "-").rjust(9)
diff --git a/sample/drb/README.ja.rdoc b/sample/drb/README.ja.rdoc
index 3ab70f3369..1697b1b704 100644
--- a/sample/drb/README.ja.rdoc
+++ b/sample/drb/README.ja.rdoc
@@ -50,7 +50,7 @@
* drbssl_s.rb
* drbssl_c.rb
-* DRbProtoclの追加例
+* DRbProtocolの追加例
* http0.rb
* http0serv.rb
diff --git a/sample/drb/README.rdoc b/sample/drb/README.rdoc
index 80ae910a8a..e6b457bc5c 100644
--- a/sample/drb/README.rdoc
+++ b/sample/drb/README.rdoc
@@ -1,6 +1,6 @@
= Sample scripts
-* array and iteretor
+* array and iterator
* darray.rb --- server
* darrayc.rb --- client
@@ -47,7 +47,7 @@
* drbssl_s.rb
* drbssl_c.rb
-* add DRbProtocl
+* add DRbProtocol
* http0.rb
* http0serv.rb
diff --git a/sample/from.rb b/sample/from.rb
index db1299c869..0e5a08de5f 100644
--- a/sample/from.rb
+++ b/sample/from.rb
@@ -62,7 +62,7 @@ def from_main
if File.exist?(file)
atime = File.atime(file)
mtime = File.mtime(file)
- open(file, "r") do |f|
+ File.open(file, "r") do |f|
until f.eof?
header = {}
f.each_line do |line|
diff --git a/sample/getoptlong/abbrev.rb b/sample/getoptlong/abbrev.rb
new file mode 100644
index 0000000000..9b89863626
--- /dev/null
+++ b/sample/getoptlong/abbrev.rb
@@ -0,0 +1,9 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', GetoptLong::NO_ARGUMENT],
+ ['--xyz', GetoptLong::NO_ARGUMENT]
+)
+options.each do |option, argument|
+ p [option, argument]
+end
diff --git a/sample/getoptlong/aliases.rb b/sample/getoptlong/aliases.rb
new file mode 100644
index 0000000000..895254c6ae
--- /dev/null
+++ b/sample/getoptlong/aliases.rb
@@ -0,0 +1,8 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', '-x', '--aaa', '-a', '-p', GetoptLong::NO_ARGUMENT]
+)
+options.each do |option, argument|
+ p [option, argument]
+end
diff --git a/sample/getoptlong/argv.rb b/sample/getoptlong/argv.rb
new file mode 100644
index 0000000000..8efcad22ea
--- /dev/null
+++ b/sample/getoptlong/argv.rb
@@ -0,0 +1,12 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--zzz', GetoptLong::NO_ARGUMENT]
+)
+puts "Original ARGV: #{ARGV}"
+options.each do |option, argument|
+ p [option, argument]
+end
+puts "Remaining ARGV: #{ARGV}"
diff --git a/sample/getoptlong/each.rb b/sample/getoptlong/each.rb
new file mode 100644
index 0000000000..661e0a968f
--- /dev/null
+++ b/sample/getoptlong/each.rb
@@ -0,0 +1,12 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', '-x', GetoptLong::REQUIRED_ARGUMENT],
+ ['--yyy', '-y', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--zzz', '-z',GetoptLong::NO_ARGUMENT]
+)
+puts "Original ARGV: #{ARGV}"
+options.each do |option, argument|
+ p [option, argument]
+end
+puts "Remaining ARGV: #{ARGV}"
diff --git a/sample/getoptlong/fibonacci.rb b/sample/getoptlong/fibonacci.rb
new file mode 100644
index 0000000000..24a2aab3c3
--- /dev/null
+++ b/sample/getoptlong/fibonacci.rb
@@ -0,0 +1,62 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
+ ['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--help', '-h', GetoptLong::NO_ARGUMENT]
+)
+
+def help(status = 0)
+ puts <<~HELP
+ Usage:
+
+ -n n, --number n:
+ Compute Fibonacci number for n.
+ -v [boolean], --verbose [boolean]:
+ Show intermediate results; default is 'false'.
+ -h, --help:
+ Show this help.
+ HELP
+ exit(status)
+end
+
+def print_fibonacci (number)
+ return 0 if number == 0
+ return 1 if number == 1 or number == 2
+ i = 0
+ j = 1
+ (2..number).each do
+ k = i + j
+ i = j
+ j = k
+ puts j if @verbose
+ end
+ puts j unless @verbose
+end
+
+options.each do |option, argument|
+ case option
+ when '--number'
+ @number = argument.to_i
+ when '--verbose'
+ @verbose = if argument.empty?
+ true
+ elsif argument.match(/true/i)
+ true
+ elsif argument.match(/false/i)
+ false
+ else
+ puts '--verbose argument must be true or false'
+ help(255)
+ end
+ when '--help'
+ help
+ end
+end
+
+unless @number
+ puts 'Option --number is required.'
+ help(255)
+end
+
+print_fibonacci(@number)
diff --git a/sample/getoptlong/permute.rb b/sample/getoptlong/permute.rb
new file mode 100644
index 0000000000..8efcad22ea
--- /dev/null
+++ b/sample/getoptlong/permute.rb
@@ -0,0 +1,12 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--zzz', GetoptLong::NO_ARGUMENT]
+)
+puts "Original ARGV: #{ARGV}"
+options.each do |option, argument|
+ p [option, argument]
+end
+puts "Remaining ARGV: #{ARGV}"
diff --git a/sample/getoptlong/require_order.rb b/sample/getoptlong/require_order.rb
new file mode 100644
index 0000000000..357f667905
--- /dev/null
+++ b/sample/getoptlong/require_order.rb
@@ -0,0 +1,13 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--zzz', GetoptLong::NO_ARGUMENT]
+)
+options.ordering = GetoptLong::REQUIRE_ORDER
+puts "Original ARGV: #{ARGV}"
+options.each do |option, argument|
+ p [option, argument]
+end
+puts "Remaining ARGV: #{ARGV}"
diff --git a/sample/getoptlong/return_in_order.rb b/sample/getoptlong/return_in_order.rb
new file mode 100644
index 0000000000..91ce1ef996
--- /dev/null
+++ b/sample/getoptlong/return_in_order.rb
@@ -0,0 +1,13 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--zzz', GetoptLong::NO_ARGUMENT]
+)
+options.ordering = GetoptLong::RETURN_IN_ORDER
+puts "Original ARGV: #{ARGV}"
+options.each do |option, argument|
+ p [option, argument]
+end
+puts "Remaining ARGV: #{ARGV}"
diff --git a/sample/getoptlong/simple.rb b/sample/getoptlong/simple.rb
new file mode 100644
index 0000000000..1af6447632
--- /dev/null
+++ b/sample/getoptlong/simple.rb
@@ -0,0 +1,7 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
+ ['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--help', '-h', GetoptLong::NO_ARGUMENT]
+)
diff --git a/sample/getoptlong/types.rb b/sample/getoptlong/types.rb
new file mode 100644
index 0000000000..ac74bfe12e
--- /dev/null
+++ b/sample/getoptlong/types.rb
@@ -0,0 +1,10 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--zzz', GetoptLong::NO_ARGUMENT]
+)
+options.each do |option, argument|
+ p [option, argument]
+end
diff --git a/sample/mine.rb b/sample/mine.rb
index a841d1a60a..77e0204bf6 100755
--- a/sample/mine.rb
+++ b/sample/mine.rb
@@ -1,6 +1,8 @@
#! /usr/bin/ruby -Ku
# -*- coding: utf-8 -*-
+require 'io/console'
+
class Board
def clr
print "\e[2J"
@@ -143,8 +145,8 @@ class Board
end
bd=Board.new(10,10,10)
-system("stty raw -echo")
-begin
+
+IO.console.raw do
loop do
case STDIN.getc
when ?n # new game
@@ -170,7 +172,5 @@ begin
bd.reset
end
end
-ensure
- system("stty -raw echo")
end
print "\n"
diff --git a/sample/mpart.rb b/sample/mpart.rb
index a88eba0ef6..eeb895d3de 100644
--- a/sample/mpart.rb
+++ b/sample/mpart.rb
@@ -2,11 +2,29 @@
# split into multi part
# usage: mpart.rb [-nnn] file..
+class MPart < File
+ def self.new(basename, extname, part, parts)
+ super(sprintf("%s.%s%02d", basename, extname, part), "w").
+ begin_mpart(basename, part, parts)
+ end
+
+ def begin_mpart(basename, part, parts)
+ printf("%s part%02d/%02d\n", basename, part, parts)
+ write("BEGIN--cut here--cut here\n")
+ self
+ end
+
+ def close
+ write("END--cut here--cut here\n")
+ super
+ end
+end
+
lines = 1000
if (ARGV[0] =~ /^-(\d+)$/ )
- lines = $1.to_i;
- ARGV.shift;
+ lines = $1.to_i
+ ARGV.shift
end
basename = ARGV[0]
@@ -14,31 +32,23 @@ extname = "part"
part = 1
line = 0
+ofp = nil
fline = 0
-for i in ifp = open(basename)
- fline = fline + 1
-end
-ifp.close
+File.foreach(basename) {fline += 1}
parts = fline / lines + 1
-for i in ifp = open(basename)
+File.foreach(basename) do |i|
if line == 0
- ofp = open(sprintf("%s.%s%02d", basename, extname, part), "w")
- printf(ofp, "%s part%02d/%02d\n", basename, part, parts)
- ofp.write("BEGIN--cut here--cut here\n")
+ ofp = MPart.new(basename, extname, part, parts)
end
ofp.write(i)
- line = line + 1
- if line >= lines and !ifp.eof?
- ofp.write("END--cut here--cut here\n")
+ line += 1
+ if line >= lines
ofp.close
- part = part + 1
+ part += 1
line = 0
end
end
-ofp.write("END--cut here--cut here\n")
ofp.close
-
-ifp.close
diff --git a/sample/trick2015/kinaba/entry.rb b/sample/trick2015/kinaba/entry.rb
index aa077dc240..2a75888ef5 100644
--- a/sample/trick2015/kinaba/entry.rb
+++ b/sample/trick2015/kinaba/entry.rb
@@ -54,8 +54,8 @@ while 0x00012345 >= $counter
srand +big && $counter >> 0b1
Enumerable
- Fixnum
- Bignum
+ String
+ Struct
Math
Complex
Comparable
diff --git a/sample/trick2018/02-mame/entry.rb b/sample/trick2018/02-mame/entry.rb
index cc4ef9cbc4..ced791aa3d 100644
--- a/sample/trick2018/02-mame/entry.rb
+++ b/sample/trick2018/02-mame/entry.rb
@@ -1,11 +1,11 @@
'';eval(r=%q(->z{r="'';eval(r=\
-%q(#{r}))[%q`#{z}`]";i=-040;30.
+%q(#{r}))[%q`#{z}`]";i=-040;31.
times{|n|(15+n%2*15-n/2).times{
r<<r[i+=(1.-n&2)*(32-n%2*31)]}}
i=r[524,0]=?\0;eval(r[479..-1])
c['"']}))[%q`GFEDCBA"+"[e\"'"'t
kE*;;\";" TRICK2018 ";tb,;{{r
-2E0$ob[us@*0)[90,336])_#i\n}s#i
+2E0$ob[us@*0)[90,336])#_i\n}s#i
0H}>["t]];};o[1,?\n*8];ex"-}eac
1Hl<1[-1]*2*t=n%2];o[14-n,0)mvk
8M$<4,?\n];15.times{|n|;o[35ie2
diff --git a/sample/trick2022/01-tompng/Gemfile b/sample/trick2022/01-tompng/Gemfile
new file mode 100644
index 0000000000..982b9de67f
--- /dev/null
+++ b/sample/trick2022/01-tompng/Gemfile
@@ -0,0 +1,2 @@
+source 'https://rubygems.org'
+gem 'matrix'
diff --git a/sample/trick2022/01-tompng/Gemfile.lock b/sample/trick2022/01-tompng/Gemfile.lock
new file mode 100644
index 0000000000..8bb3c69025
--- /dev/null
+++ b/sample/trick2022/01-tompng/Gemfile.lock
@@ -0,0 +1,13 @@
+GEM
+ remote: https://rubygems.org/
+ specs:
+ matrix (0.4.2)
+
+PLATFORMS
+ x86_64-darwin-20
+
+DEPENDENCIES
+ matrix
+
+BUNDLED WITH
+ 2.3.3
diff --git a/sample/trick2022/01-tompng/authors.markdown b/sample/trick2022/01-tompng/authors.markdown
new file mode 100644
index 0000000000..26ebe24da6
--- /dev/null
+++ b/sample/trick2022/01-tompng/authors.markdown
@@ -0,0 +1,3 @@
+* Tomoya Ishida (tompng)
+ * tomoyapenguin@gmail.com
+ * cctld: jp
diff --git a/sample/trick2022/01-tompng/entry.rb b/sample/trick2022/01-tompng/entry.rb
new file mode 100644
index 0000000000..97beacc684
--- /dev/null
+++ b/sample/trick2022/01-tompng/entry.rb
@@ -0,0 +1,40 @@
+ eval((s=%~c=(0..35
+ ).map{s[2*_1+1]}*'';class$Inte
+ ger;def$quXinclude(Math ;spXo(a)=self*
+ a.pow(87X=h=32.chr;g=PI/480;ls=(sp*31X,89)%89;
+ def$abX+'eval((s=%'+(n=? .next)+s*88.chr+[nXs()=[a
+ =self%X+'.split(',sp*25+'?'+88.chr+');(0..36).mapX89,89-
+ a].miX{s[2*_1].split}',sp*31+".join.tr('$',$/)))"]*$/)Xn;end
+ ;reqX.split$/;trap(:INT){puts;exit};q=->t,i{a,y=((t+i*99)Xuire
+ 'matrX%960). ivmod(80);[(a*(7+i)+i*23)%79+(y+a)/(5+i%4)%2,39Xix';1
+ 5.tiX-y/2]};p=->t,u{a=->b,c{(0..5).sum{(u%2-1)*E**(t*(b+c*_1)*gXmes{
+ |i,*X.i+ i*u+=5+sin(u*u))}};x,z=a[5,3]. 5,3].rect;x+=y.Xv|z=
+ *?!Xi a[19,4];z+=w;r=(4+(x.abs+z.i).ab };t=(0..959).fX..?
+ W,?Xind{|t|(0..29).all?{x,y=q[t,_1];(x 2||h=ls[y][x]X[,*
+ ?]..X[/[^!-}]/]}};h=($**h+h).chr;eval( []} ->(x,yX?};a
+ =(0X,a,b){x=x*36+39.5;y=19.5-y*18;b*=1 |i|((yX..1
+ 34)X-b).ceil..y+b).map{|j|((x-i)/a+(y j)/ .times{X.ma
+ p{zXx,z=p[t,_1];l=u```=0;while``````(l<1)``; u+```=0 ;d=x-y;X.in
+dex(Xl+=(d.abs+(z-w``)``.i).ab``s*1.``1 ;x``,z=y``,w;o[v``=``x.r d.imag/Xc[i+
+15*Xd.abs*l*sin(2*``l-t``*g*80``-_1) l*(``1-l)/``6,a``=l*( -l)**2*0.X_1]
+)};X7,a*2]&&o[v,z,``0.0``3,l**`` ``times``{|i``|(8+i).times{|Xw=*
+MatXj|o[sin(i)/2+````` ```sin( `/2.0`````````)*j/200,j*0.0Xrix
+[*(X5-1,0.02,0.1]``}} ``. q[t,``_1];m``[y][x]= };i=-X0..
+44).X1;$><<(['%%','[H .map{|j|(0..79).map{|k|x=(Xmap{
+ |i,X -39.5)/35.8;y=( i+=1;m[j][k]?h:c[i]):ls[j]X*b|
+ v<<X[k];}*''}*$/<<0) 1)%9 te"`")#qv.jSaL{=;q(Q}4fXa.z
+ ip(Xjs(:#tK`Jm))FKO /A9(2'%iorvf7 eEa0uV xv+Q@qUU](L@&Py .1v'X0..
+ ).suXydSEH{-GI|-5(,z G5evpq,[b50 D[ t {on,I?VStS`?G@LoqFCXm{|j
+ ,k|Xj1.QnxKz!mH%o# )b2Seut,]! 48 lBieJGi 5jeNPD#b}H3X-(p
+ =(iXaVz#8*+US,hgF 5#6]y-` 4hy HN hF75WjD!0IxJ$sX+k)
+ .powX+UP"cNUE9- G< tHvV;Ib <-s U T ? vlE xylg=x#X(i+k
+ ,88)XV9u$9lKb9 @C do7+-w >l { v9 { P l ga%]AK<e&'X+1)*
+ (j||(X4ifK/6S+ k} @@*a} 6rS xn"Q[M 8 `|g>$#BrjXb<<p;
+ 0))}XtbDp'Kc t2 Dat9C s C rL+ g,j]Tf B< eMI+zzkWX;b}]
+ .lup.XtVP<ak IM E/+)B jwv uB (Twqed D* dyf_dT7Xsolve
+ (v);13Xn:8 #_ RiSTO, [Fk m O]O#"+ a_ cT_.X5.time
+ s{c[i+X e5 T`FBEC q*f 2 o@{a<eUG aW PX15*_1]
+ =z[a[_1]X z_@`nll 7F1 2 [=^uS0z^ 6X||w.shif
+ t]}};eval(Xfg K#R N bp-E_Xc)~.split(
+ ?X);(0..36).map{s[2*_1].split}
+ .join.tr('$',$/)))
diff --git a/sample/trick2022/01-tompng/remarks.markdown b/sample/trick2022/01-tompng/remarks.markdown
new file mode 100644
index 0000000000..70601908b7
--- /dev/null
+++ b/sample/trick2022/01-tompng/remarks.markdown
@@ -0,0 +1,51 @@
+### Remarks
+
+Just run it with no argument:
+
+ ruby entry.rb
+
+Or run it with one non-ascii half-width character argument:
+
+ ruby entry.rb ⬮
+ ruby entry.rb 𓆡
+
+I confirmed the following implementations/platforms:
+
+* ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
+* ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin20]
+
+### Description
+
+This program is an aquatic quine.
+Some characters in the code are overwritten with `" "`, but this program can restore the missing parts.
+Every frame of this animation is an executable ruby program that let fishes start swimming again from their current position.
+
+### Internals
+
+#### Error Correction
+
+Error correction is performed for each block of length 135.
+It consists of 89 kinds of characters(`[*('!'..'W'), '[', *(']'..'}')]`) and satisfies the following constraint.
+
+```
+matrix(size: 45x135) * block_vector(size: 135) % 89 == zero_vector(size: 45)
+```
+
+To restore the missing characters in the block, we need to solve a linear equation problem in modulo 89.
+This can be achieved by using bundled gem 'matrix' and overwriting some methods.
+
+```ruby
+require 'matrix'
+matrix = Matrix[[3, 1, 4], [1, 5, 9], [2, 6, 5]]
+class Integer
+ def quo(x) = self * x.pow(87, 89) % 89 # Fermat's little theorem. 89 is a prime number.
+ def abs() = [self % 89, 89 - self % 89].min # To avoid division by multiple of 89.
+end
+answer = matrix.lup.solve([1, 2, 3]) #=> Vector[24, 42, 83]
+(matrix * answer).map { _1 % 89 } #=> Vector[1, 2, 3]
+```
+
+#### Resuming Animation
+
+The entire animation of this fish tank is a loop of 960 frames.
+This program uses position of the floating bubbles to detect current frame number from the executed source code.
diff --git a/sample/trick2022/02-tompng/authors.markdown b/sample/trick2022/02-tompng/authors.markdown
new file mode 100644
index 0000000000..26ebe24da6
--- /dev/null
+++ b/sample/trick2022/02-tompng/authors.markdown
@@ -0,0 +1,3 @@
+* Tomoya Ishida (tompng)
+ * tomoyapenguin@gmail.com
+ * cctld: jp
diff --git a/sample/trick2022/02-tompng/entry.rb b/sample/trick2022/02-tompng/entry.rb
new file mode 100644
index 0000000000..2e2e2bcf74
--- /dev/null
+++ b/sample/trick2022/02-tompng/entry.rb
@@ -0,0 +1,32 @@
+ q=->{!sleep _1/1e2};p=(
+ c=0..2).map{[_1/9r ,0,5**_1.i/3,1,0]}
+ require'socket';puts'op' "en http://localhost:#{(
+ w=TCPServer.new$*[0]||0).addr[1]}";Thread.new{q[2];f=[-1
+ ]*s=3;t=Time.now.to_f;p.select!{0<_1[3]=[_1[3]+_1[4]/8.0,1
+ ].min};9.times{h=p.map{[2**(_1*t.i)/_4**0.5/(1+Math.sin(2*t-
+ 9*_1%2)**32/16),_2+_4*( _3-_2)]};r=[s*3/2,84].min;g=->{x,y=
+(s*(1+_1+1i)/2).rect;x<0 ||x>=s-1||y<0||y>=s-1?0:((l=f[y+1])[
+x+1]*(a=x%1)+(1-a)*l[x] )*(b=y%1)+(1-b)*((l=f[y])[x+1]*a+(1-
+a)*l[x])};f=(1..r).map {|y|(1..r).map{|x|z=1.5+1.5i-3.0*(y
+.i+x)/r;[h.sum{g[_1.*z +_2]}*0.9,1].min}};s=r};c=f.flatten
+redo};loop{s=w.accept ; Thread.new{r=s.gets;h='HTTP/1.1 '+
+"200 OK\r\nContent-" 'T' "ype:text/html\r\n\r\n";r['/ ']?s.
+ <<(h+'<style>ifram' 'e{' 'opacity:0;height:0;}input{wid'+
+ 'th:252px;}</styl' 'e>' '<form target="i"><input src="'+
+ "g#{rand}\" type" '="im' 'age"><iframe name="i"></ifra'+
+ 'me></form>'):r ['/g'] ?(h[/:.+l/]=?:'image/gif';s<<
+ h+'GIF8' '7a'+[84,
+ 84,246,0,*(0..383).map {15*_1. /(383r)**(3-_1%
+ 3)*17}].pack('v3c*'); loop{ s<<[67434785,5,
+ 44,84,84,7,c.map{_1* 127} .each_slice(126
+ ).map{[127,128,*_1 ] .pack'c*'}*'',
+ 1,129].pack('V3x' 'v2na*c2x');q[
+ 5];q.[]1while(r ==r=c)}):(x,y,
+ z=r.scan(/\d+/).map{_1.to_f/
+ 126-1};z&&p<<[rand-0.5,(
+ z=x+y.i)*1.5,z/(z.
+ abs+0.9),0,-p[
+ -3][4]=-1]
+ s.<<h);s
+ .close
+ }}
diff --git a/sample/trick2022/02-tompng/remarks.markdown b/sample/trick2022/02-tompng/remarks.markdown
new file mode 100644
index 0000000000..3b2d3fd84b
--- /dev/null
+++ b/sample/trick2022/02-tompng/remarks.markdown
@@ -0,0 +1,32 @@
+### Remarks
+
+1. Run it with `ruby entry.rb 8080`
+
+2. Open "http://localhost:8080"
+
+3. Click on the screen and interact with it
+
+I confirmed the following implementations/platforms:
+
+* Ruby Version
+ * ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-darwin20]
+* Browser
+ * Chrome(macOS, Android)
+ * Firefox(macOS)
+ * Edge(macOS)
+
+### Description
+
+This program is an HTTP server that provides a fractal creature playground.
+You can see the heartbeat of a mysterious fractal creature. Clicking on the screen will change the shape of the creature.
+Surprisingly, this interactive webpage is built without JavaScript.
+
+### Internals
+
+Fractal: Iterated function system
+Rendering from server: Streaming animated GIF
+Sending click event to server: `<input type="image" src="streaming.gif">` with `<form target="invisible_iframe">`
+
+### Limitations
+
+Does not work on Safari and iOS.
diff --git a/sample/trick2022/03-mame/authors.markdown b/sample/trick2022/03-mame/authors.markdown
new file mode 100644
index 0000000000..0e420fdf5d
--- /dev/null
+++ b/sample/trick2022/03-mame/authors.markdown
@@ -0,0 +1,3 @@
+* Yusuke Endoh
+ * mame@ruby-lang.org
+ * cctld: jp
diff --git a/sample/trick2022/03-mame/entry.rb b/sample/trick2022/03-mame/entry.rb
new file mode 100644
index 0000000000..f24595dfa9
--- /dev/null
+++ b/sample/trick2022/03-mame/entry.rb
@@ -0,0 +1,27 @@
+2022;"#
+
+.chars} {puts'TRICK+2022'
+ \ { ;
+#';$><< b
+ ?!
+};#{s=' ' # 0
+! s[0]? ( b=$<.read ;'
+} ub( ,''}
+';a= ''<<32
+b.lines {puts( ?.. *(
+b.size) .gsub(/./) {
+b.sub!( /^#$`\K(\S)
+ /x,a)?$1:a }
+ .rstrip)}):
+ ( [ 12,1,12,11].
+cycle { | i | t = ( s *
+
+10<<
+10)*
+10+ %(\e[A)*
+10
+10. times{t[i*
+ _1 ] = 'TRICK+2022'[
+ _1 ] };$><<t
+ sleep 1})
+ }"
diff --git a/sample/trick2022/03-mame/remarks.markdown b/sample/trick2022/03-mame/remarks.markdown
new file mode 100644
index 0000000000..c38279f016
--- /dev/null
+++ b/sample/trick2022/03-mame/remarks.markdown
@@ -0,0 +1,96 @@
+Execute the program normally.
+
+```
+$ ruby entry.rb
+```
+
+It shakes a string.
+
+... Wait! This is not all.
+
+Next, please apply "leftward gravity" to each letter in the file.
+IOW, if there is a space to the left of a letter, move it to the left.
+Here, you may want to use the following command.
+
+```
+$ sed "s/ //g" entry.rb | tee up.rb
+```
+
+This program applies "upward gravity" to each letter in an input text.
+The following demo will help you understand what this means.
+
+```
+$ cat test.txt
+$ ruby up.rb test.txt
+```
+
+Now, here's where we come in.
+Please apply "upward gravity" to entry.rb.
+
+```
+$ ruby up.rb entry.rb | tee left.rb
+```
+
+I think that you already noticed that.
+This program applies "leftward gravity" to an input text.
+
+```
+$ cat test.txt
+$ ruby left.rb test.txt
+```
+
+`sed` is no longer required to create `up.rb`; just use `left.rb`.
+
+```
+$ ruby left.rb entry.rb > up.rb
+```
+
+We've come to the final stage.
+Please apply `left.rb` to `left.rb`.
+
+```
+$ ruby left.rb left.rb | tee horizontal.rb
+$ ruby horizontal.rb
+```
+
+Of course, it is also possible to apply `up.rb` to `up.rb`.
+
+```
+$ ruby up.rb up.rb | tee vertical.rb
+$ ruby vertical.rb
+```
+
+Can you tell how they work? Enjoy analyzing!
+
+
+
+---
+Code reading tips (spoiler)
+
+Some code fragments are highly reused between the programs.
+For example, note that this program has one code fragment to input a text
+(`b=$>.read`); `up.rb` and `left.rb` share and use this code fragment.
+Also, `horizontal.rb` and `vertical.rb` share the fragment `puts'TRICK+2022'`.
+Sometimes letters in very distant places are reused all over the place.
+
+Can you tell how it detects if it is already aligned or not yet?
+Here is a simplified version of the gimmick to switch behavior when
+left-aligned:
+
+```
+"\ #{puts('not left-aligned yet')}
+ # {puts('left-aligned')}"
+```
+
+And for top-aligned:
+
+```
+"#
+xx{puts('top-aligned')}
+x#{puts('not top-aligned yet')}
+"
+```
+
+It is also necessary to detect "top-left-aligned" and "left-top-aligned".
+I made tons of subtle adjustments and trial-and-error to create the program.
+I no longer know precisely how it works.
diff --git a/sample/trick2022/03-mame/test.txt b/sample/trick2022/03-mame/test.txt
new file mode 100644
index 0000000000..18802153a9
--- /dev/null
+++ b/sample/trick2022/03-mame/test.txt
@@ -0,0 +1,13 @@
+T
+ R
+ I
+ C
+ K
+ |
+ |
+ |
+ |
+ 2
+ 0
+ 2
+ 2
diff --git a/sample/trick2022/README.md b/sample/trick2022/README.md
new file mode 100644
index 0000000000..3b2af6f86b
--- /dev/null
+++ b/sample/trick2022/README.md
@@ -0,0 +1,14 @@
+This directory contains the award-winning entries of
+the 4th Transcendental Ruby Imbroglio Contest for rubyKaigi (TRICK 2022).
+
+THESE ARE BAD EXAMPLES! You must NOT use them as a sample code.
+
+* 01-tompng/entry.rb: "Best fishbowl" -- Tomoya Ishida (tompng)
+* 02-tompng/entry.rb: "Most interactive code" -- Tomoya Ishida (tompng)
+* 03-mame/entry.rb: "Most anti-gravity" -- Yusuke Endoh
+
+These files are licensed under MIT license.
+
+For the contest outline and other winning entries, see:
+
+https://github.com/tric/trick2022
diff --git a/sample/uumerge.rb b/sample/uumerge.rb
index 2576bcb864..1b81582c24 100644
--- a/sample/uumerge.rb
+++ b/sample/uumerge.rb
@@ -15,7 +15,7 @@ while line = gets()
if out_stdout
out = STDOUT
else
- out = open($file, "w") if $file != ""
+ out = File.open($file, "w") if $file != ""
end
out.binmode
break