Skip to content

Instantly share code, notes, and snippets.

View taq's full-sized avatar

Eustáquio Rangel taq

View GitHub Profile
@taq
taq / binreaddouble.sh
Created April 12, 2012 20:14
Lendo um double do arquivo, em shell.
od --skip-bytes=4 --read-bytes=8 -t fD -An teste.bin | printf "%.0f" $(tr -d ' ')
@taq
taq / htmlescape.rb
Created May 18, 2012 21:39
HTML escape
#!/bin/ruby
require "cgi"
STDOUT.print CGI.escapeHTML(ARGV.size>0 ? File.read(ARGV[0]) : STDIN.read)
@taq
taq / htmlescape.html
Created May 18, 2012 21:40
HTML escape test
<!DOCTYPE HTML>
<html>
<head>
<title>HTML escape test</title>
</head>
<body>
<p>This is a test.</p>
</body>
</html>
@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 / 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 / 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 / prepend2.rb
Created November 3, 2012 19:06
Ruby 2.0 Module#prepend
class C
def x; "x"; end
end
module M
def x; '[' + super + ']'; end
def y; "y"; end
end
class C
@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
@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 / 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