initialize @files = [] end def <<(filename) content = ::File.read(filename) @files << File.new(filename, content) self end def each @files.each do |file| yield([file.name, file.content]) end end end
initialize @files = [] end def <<(filename) content = ::File.read(filename) @files << File.new(filename, content) self end def each @files.each do |file| yield([file.name, file.content]) end end end files = MyFileChanger.new files << '/etc/profile' << '/etc/bashrc' files.each_with_index do |(name, content), index| puts "#{index}. #{name} is #{content.bytesize} bytes" end names = files.map do |(name, content)| name end puts names.inspect size = files.reduce(0) do |sum, (name, content)| sum + content.bytesize end puts size
initialize @files = [] end def <<(filename) content = ::File.read(filename) @files << File.new(filename, content) self end def each @files.each do |file| yield([file.name, file.content]) end end end files = MyFileChanger.new files << '/etc/profile' << '/etc/bashrc' files.each_with_index do |(name, content), index| puts "#{index}. #{name} is #{content.bytesize} bytes" end names = files.map do |(name, content)| name end puts names.inspect size = files.reduce(0) do |sum, (name, content)| sum + content.bytesize end puts size 0. /etc/profile is 189 bytes 1. /etc/bashrc is 745 bytes ["/etc/profile", "/etc/bashrc"] 934
n } Enumerator.new(a).each { |n| puts n } naturals = Enumerator.new do |y| n = 1 loop do y << n n += 1 end end odds = Enumerator.new do |y| naturals.each do |n| y << n if n % 2 != 0 end end
n } Enumerator.new(a).each { |n| puts n } naturals = Enumerator.new do |y| n = 1 loop do y << n n += 1 end end odds = Enumerator.new do |y| naturals.each do |n| y << n if n % 2 != 0 end end class Integer def prime? return false if self < 2 max = Math.sqrt(self).floor (2..max).none? { |n| self % n == 0 } end end primes = Enumerator.new do |y| y << 2 odds.each do |n| y << n if n.prime? end end
n } Enumerator.new(a).each { |n| puts n } naturals = Enumerator.new do |y| n = 1 loop do y << n n += 1 end end odds = Enumerator.new do |y| naturals.each do |n| y << n if n % 2 != 0 end end class Integer def prime? return false if self < 2 max = Math.sqrt(self).floor (2..max).none? { |n| self % n == 0 } end end primes = Enumerator.new do |y| y << 2 odds.each do |n| y << n if n.prime? end end puts primes.take(10).inspect # => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
i.to_s } GC.start end puts results # => 0.480000 0.010000 0.490000 ( 0.487643) n = 5000000 # Simple benchmark Benchmark.bm do |x| x.report { for i in 1..n; a = "1"; end } x.report { n.times do ; a = "1"; end } x.report { 1.upto(n) do ; a = "1"; end } end
for i in 1..n; a = "1"; end } x.report('times:') { n.times do ; a = "1"; end } x.report('upto:') { 1.upto(n) do ; a = "1"; end } end # Warmup Benchmark.bmbm(10) do |x| x.report('for:') { for i in 1..n; a = "1"; end } x.report('times:') { n.times do ; a = "1"; end } x.report('upto:') { 1.upto(n) do ; a = "1"; end } end
else acc(i - 1, n + result, n) end end def fib(i) acc(i, 1, 0) end puts fib(10000) # => stack level too deep (SystemStackError) http://timelessrepo.com/tailin-ruby
} RubyVM::InstructionSequence.new(<<-EOF).eval def acc(i, n, result) if i == -1 result else acc(i - 1, n + result, n) end end def fib(i) acc(i, 1, 0) end EOF
} RubyVM::InstructionSequence.new(<<-EOF).eval def acc(i, n, result) if i == -1 result else acc(i - 1, n + result, n) end end def fib(i) acc(i, 1, 0) end EOF puts fib(10000) # => A number with 2090 digits
serve(io) cmd = io.read(4).strip case cmd when 'hex' then io.puts(SecureRandom.hex) when 'uuid' then io.puts(SecureRandom.uuid) end end end server = RandomServer.new(10101) server.start server.join
capture("randomizer #{pattern.shellescape} /dev/*") # Assuming our capture function could take *args like system capture('randomizer', pattern, '/dev/*') # => Nope
= true store.transaction do store['App'] = 'The Randomizer' store[:randoms] ||= [] store[:randoms] << Randomizer.uuid end store.transaction do puts store[:randoms].inspect # => ["901dcd24-fa10-4194-b25b-8ad49bc875fb"] end
= true store.transaction do store['App'] = 'The Randomizer' store[:randoms] ||= [] store[:randoms] << Randomizer.uuid end store.transaction do puts store[:randoms].inspect # => ["901dcd24-fa10-4194-b25b-8ad49bc875fb"] end store.transaction do store[:randoms] = [] store.abort # Or an Exception end
= true store.transaction do store['App'] = 'The Randomizer' store[:randoms] ||= [] store[:randoms] << Randomizer.uuid end store.transaction do puts store[:randoms].inspect # => ["901dcd24-fa10-4194-b25b-8ad49bc875fb"] end store.transaction do store[:randoms] = [] store.abort # Or an Exception end store.transaction do puts store[:randoms].inspect # => ["901dcd24-fa10-4194-b25b-8ad49bc875fb"] end
2, 1 + 1 end end describe Integer do it 'should subtract' do (1 - 1).must_equal(0) end end describe 'Mocks' do it 'should mock things' do mock = MiniTest::Mock.new mock.expect(:length, 10) mock.length.must_equal(10) mock.verify end end
for a bunch of threads, block called for each when it finishes. • Queue/SizedQueue => require ‘thread’ • << things in, #pop things out. • MonitorMixin => require ‘monitor’ • Thread safety with synchronize block.