summaryrefslogtreecommitdiff
path: root/ruby
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-14 16:46:27 +0200
committerPatrick Simianer <p@simianer.de>2014-06-14 16:46:27 +0200
commit26c490f404731d053a6205719b6246502c07b449 (patch)
tree3aa721098f1251dfbf2249ecd2736434c13b1d48 /ruby
init
Diffstat (limited to 'ruby')
-rwxr-xr-xruby/blocks.rb15
-rwxr-xr-xruby/call_by_reference.rb14
-rwxr-xr-xruby/class_method.rb36
-rwxr-xr-xruby/cmp.rb29
-rwxr-xr-xruby/extend_array.rb12
-rwxr-xr-xruby/lowercase.rb10
-rwxr-xr-xruby/modules.rb16
-rwxr-xr-xruby/spawn.rb10
-rwxr-xr-xruby/spawn_with_timeout.rb17
-rwxr-xr-xruby/threads.rb37
-rwxr-xr-xruby/timeout.rb18
11 files changed, 214 insertions, 0 deletions
diff --git a/ruby/blocks.rb b/ruby/blocks.rb
new file mode 100755
index 0000000..9cea44f
--- /dev/null
+++ b/ruby/blocks.rb
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+
+
+def a
+ i = 0
+ while line = STDIN.gets
+ yield i, line
+ i += 1
+ end
+end
+
+a { |i,j|
+ puts "#{i} #{j}"
+}
+
diff --git a/ruby/call_by_reference.rb b/ruby/call_by_reference.rb
new file mode 100755
index 0000000..12dc933
--- /dev/null
+++ b/ruby/call_by_reference.rb
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+
+
+def fun w
+ w << "x"
+end
+
+
+w = [1,3,4,5]
+
+fun w
+
+puts w.to_s
+
diff --git a/ruby/class_method.rb b/ruby/class_method.rb
new file mode 100755
index 0000000..447a477
--- /dev/null
+++ b/ruby/class_method.rb
@@ -0,0 +1,36 @@
+#!/usr/bin/env ruby
+
+
+class A
+ attr_accessor :z
+
+ def initialize
+ self.z = 'a'
+ end
+
+ def self.a
+ r = self.new
+ return r
+ end
+end
+
+class B < A
+
+ def initialize
+ self.z = 'b'
+ end
+
+ def b
+ "b"
+ end
+end
+
+a = A.new
+b = B.new
+
+puts a.z
+puts b.z
+puts
+x = B.a
+puts x.z
+
diff --git a/ruby/cmp.rb b/ruby/cmp.rb
new file mode 100755
index 0000000..65e5c5f
--- /dev/null
+++ b/ruby/cmp.rb
@@ -0,0 +1,29 @@
+#!/usr/bin/env ruby
+
+
+class A
+ attr_accessor :a
+
+ def initialize a
+ @a = a
+ end
+
+ def == other
+ @a.size == other.a.size
+ end
+ alias :eql? :==
+
+ def hash
+ @a.hash
+ end
+end
+
+a = A.new 'a'
+b = A.new 'b'
+ax = A.new 'a'
+
+puts a == ax
+
+l = [a, b, ax]
+puts l.uniq.size
+
diff --git a/ruby/extend_array.rb b/ruby/extend_array.rb
new file mode 100755
index 0000000..9b00917
--- /dev/null
+++ b/ruby/extend_array.rb
@@ -0,0 +1,12 @@
+#!/usr/bin/env ruby
+
+
+class Array
+ def sum
+ self.inject(:+)
+ end
+end
+
+a = [1,2,3]
+puts a.sum
+
diff --git a/ruby/lowercase.rb b/ruby/lowercase.rb
new file mode 100755
index 0000000..52c1097
--- /dev/null
+++ b/ruby/lowercase.rb
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+STDIN.set_encoding 'utf-8'
+STDOUT.set_encoding 'utf-8'
+
+
+while line = STDIN.gets
+ STDOUT.write line.downcase
+end
+
diff --git a/ruby/modules.rb b/ruby/modules.rb
new file mode 100755
index 0000000..673bd82
--- /dev/null
+++ b/ruby/modules.rb
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+
+
+module X
+ def self.f
+ return 42
+ end
+
+ def X::g
+ return 23
+ end
+end
+
+puts X::f
+puts X::g
+
diff --git a/ruby/spawn.rb b/ruby/spawn.rb
new file mode 100755
index 0000000..8ee810a
--- /dev/null
+++ b/ruby/spawn.rb
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+
+cmd = "for i in {1..30}; do echo '123456789'; done"
+pipe_in, pipe_out = IO.pipe
+pid = Process.spawn(cmd, :out => pipe_out, :err => pipe_out)
+Process.wait pid
+pipe_out.close
+puts pipe_in.read
+
diff --git a/ruby/spawn_with_timeout.rb b/ruby/spawn_with_timeout.rb
new file mode 100755
index 0000000..a884673
--- /dev/null
+++ b/ruby/spawn_with_timeout.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+
+require 'timeout'
+
+
+cmd = "for i in {1..2000000}; do echo '123456789'; done"
+pipe_in, pipe_out = IO.pipe
+pid = Process.spawn(cmd, :out => pipe_out, :err => pipe_out)
+begin
+ Timeout.timeout(1) { Process.wait pid }
+rescue Timeout::Error
+ puts 'process not finished in time, killing it'
+ Process.kill('TERM', pid)
+end
+pipe_out.close
+puts pipe_in.read
+
diff --git a/ruby/threads.rb b/ruby/threads.rb
new file mode 100755
index 0000000..ca2d0b2
--- /dev/null
+++ b/ruby/threads.rb
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby
+
+require 'thread'
+
+
+prev = 'z'
+('a'..'r').each { |i|
+ Marshal.dump({i=>1,prev=>1,'x'=>1}, File.new("test.#{i}", 'wb'))
+ prev = i
+}
+
+a = []
+threads = []
+mutex = Mutex.new
+('a'..'r').each { |i|
+ threads << Thread.new(i) { |c|
+ mutex.synchronize { puts c }
+ h = Marshal.load(File.new('test.'+c, 'rb'))
+ puts "#{c} x"
+ mutex.synchronize { a << h }
+ }
+}
+threads.each { |t| t.join }
+puts '--'
+threads = []
+while a.size > 1
+ threads << Thread.new {
+ i = j = nil
+ mutex.synchronize { i = a.pop; j = a.pop }
+ i.merge!(j) { |k,v,w| v+w }
+ mutex.synchronize { a << i }
+ }
+ threads.each { |t| t.join }
+end
+
+File.open('counts.merged', 'wb') { |f| Marshal.dump(a[0], f) }
+
diff --git a/ruby/timeout.rb b/ruby/timeout.rb
new file mode 100755
index 0000000..f5c1e7e
--- /dev/null
+++ b/ruby/timeout.rb
@@ -0,0 +1,18 @@
+#!/usr/bin/env ruby
+
+require 'timeout'
+
+
+puts 'starting process'
+pid = Process.spawn('sleep 20')
+begin
+ Timeout.timeout(5) do
+ puts 'waiting for the process to end'
+ Process.wait(pid)
+ puts 'process finished in time'
+ end
+rescue Timeout::Error
+ puts 'process not finished in time, killing it'
+ Process.kill('TERM', pid)
+end
+