Skip to content

Instantly share code, notes, and snippets.

View taq's full-sized avatar

Eustáquio Rangel taq

View GitHub Profile
#!/bin/bash
if ! which md5sum > /dev/null; then
echo Install md5sum
exit 1
fi
if ! which curl > /dev/null; then
echo Install curl
exit 1
@taq
taq / spreadsheet_test.rb
Created November 24, 2015 19:03
Minitest with before_all
require "minitest/autorun"
require "minitest/spec"
require "spreadsheet"
describe 'spreadsheet' do
def self.before_all
@doc ||= Spreadsheet.open "spreadsheet.xls"
end
before do
@taq
taq / ruby-version.bash
Created November 17, 2010 18:13
Ruby version Bash function
# First parameter is a bash printf formatting string
# From second till fifth parameter, rvm-prompt format parameters
__ruby_ps1 () {
if [ ! -f ./Rakefile ] &&
[ "$(find -maxdepth 1 -name '*.rb' | head -n1)" == "" ]; then
exit 1
fi
if [ -f ~/.rvm/bin/rvm-prompt ]; then
rst=$(~/.rvm/bin/rvm-prompt $2 $3 $4 $5)
fi
@taq
taq / enum_perf_big.rb
Created November 3, 2012 19:46
Ruby 2.0 lazy enumerators bigger collection performance
require "benchmark"
include Benchmark
values = (0..1000).to_a
bm(10) do |bench|
bench.report("regular") do
values.map { |x| x * 10 }.select { |x| x > 30 }
end
bench.report("lazy") do
@taq
taq / onigmo.rb
Created November 3, 2012 19:25
Ruby 2.0 Onigmo
regexp = /^([A-Z])?[a-z]+(?(1)[A-Z]|[a-z])$/
p regexp =~ "foo" #=> 0
p regexp =~ "foO" #=> nil
p regexp =~ "FoO" #=> 0
@taq
taq / prepend.rb
Created November 3, 2012 19:02
Module#prepend - using include on 1.9.x
class C
def x; "x"; end
end
module M
def x; '[' + super + ']'; end
def y; "y"; end
end
class C
@taq
taq / keyword.rb
Created November 3, 2012 18:53
Ruby 2.0 keyword arguments
def foo(str: "foo", num: 123456)
[str, num]
end
p foo(str: 'buz', num: 9) #=> ['buz', 9]
p foo(str: 'bar') # => ['bar', 123456]
p foo(num: 123) # => ['foo', 123]
p foo # => ['foo', 123456]
p foo(bar: 'buz') # => ArgumentError
@taq
taq / symbol_array.rb
Created November 3, 2012 18:47
Ruby 2.0 symbol array literal
p [:ruby, :"2.0", :symbol, :array, :literal]
#=> [:ruby, :"2.0", :symbol, :array, :literal]
p %i(ruby 2.0 symbol array literal)
#=> [:ruby, :"2.0", :symbol, :array, :literal]
@taq
taq / enum.rb
Created November 3, 2012 19:34
Natural numbers enumerator on Ruby 1.9.x
natural_numbers = Enumerator.new do |yielder|
number = 1
loop do
yielder.yield number
number += 1
end
end
p natural_numbers.select { |n| n.odd? }.take(5).to_a
@taq
taq / refinements.rb
Created November 3, 2012 19:16
Ruby 2.0 refinements
module TimeExtensions
refine Fixnum do
def minutes; self * 60; end
end
end
class MyApp
using TimeExtensions
def initialize
p 2.minutes