This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ type rvm | head -n1 | |
rvm is a function | |
$ screen | |
(now inside a fresh screen) | |
$ type rvm | head -n1 | |
rvm is /usr/local/rvm/bin/rvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
class Foo < Sinatra::Base | |
end | |
Foo.inline_templates=nil | |
p Foo.templates[:thing] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
get "/" do | |
content_type "text/plain" | |
txt = "" | |
env.each {|k,v| txt += "\n%30s: %s" % [k,v]} | |
txt | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# converts an arbitrary decimal number into a fraction | |
# returns [input, result, check] where "check" is eval(result) with floats | |
def frac(str); raise ArgumentError unless str =~ /\A(\d*)\.(\d+)\z/; x=($1+$2).to_i; y=10**($2.size); gcd=x.gcd(y); r="#{x/gcd}/#{y/gcd}"; return [str, r, eval(r+".0")]; end | |
# testing it with some random examples: | |
p frac("#{rand 100}.#{rand 10000}") | |
# => ["39.3575", "15743/400", 39.3575] | |
# => ["34.7117", "347117/10000", 34.7117] | |
# => ["41.8720", "5234/125", 41.872] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bits(x) "%064b" % ([x].pack("G").unpack("Q>")[0]) end | |
bits(0.1) | |
#=> "0011111110111001100110011001100110011001100110011001100110011010" | |
bits(-0.1) | |
#=> "1011111110111001100110011001100110011001100110011001100110011010" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailTemplate | |
def initialize | |
@io = StringIO.new | |
end | |
def puts(*args) | |
@io.puts *args | |
end | |
def print(*args) | |
@io.print *args | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style type="text/css"> | |
#tabnav { | |
list-style-type: none; | |
padding: 0; | |
} | |
#tabnav li { | |
display: inline; | |
padding: 0.5em; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# constant-time comparison algorithm to prevent timing attacks | |
# (borrowed from devise) | |
def secure_compare(a, b) | |
return false if a.bytesize != b.bytesize | |
l = a.unpack "C#{a.bytesize}" | |
res = 0 | |
b.each_byte { |byte| res |= byte ^ l.shift } | |
res == 0 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
not_found do | |
# redirect GET and HEAD requests to "/foo/" to "/foo" | |
if (request.get? || request.head?) && request.path_info[-1] == "/" | |
newpath = request.path_info.chop | |
if Sinatra::Application.routes["GET"].find {|re,*other| newpath =~ re } | |
redirect to(newpath) | |
end | |
end | |
haml :x404 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
DATA = File.read("data.txt") | |
get "/" do | |
"I have some data for you: #{DATA}" | |
end |
OlderNewer