Skip to content

Instantly share code, notes, and snippets.

class AuthEngine < Sinatra::Base
set :sessions => true
register do
def auth type
condition do
redirect "/login" unless send "is_#{type}?"
end
end
@acook
acook / user_model.rb
Created August 26, 2013 02:08
In-Memory User Model, can be persisted later.
require 'hashie'
class User < Hashie::Dash
class << self
def authenticate user, pass
where(username: user, password: pass).first
end
def create attributes = {}
new_user = new attributes
class SingleCharacterAttribtue
attr :my_single_character_attribute
def my_single_character_attribute = single_character
raise ArgumentError, "Expected single character, got #{single_character.length} characters." unless single_character.length = 1
@mysingle_character_attribute = single_character
end
end
# usage
letter_pairs = [%w{a b}, %w{c d}]
letter_pairs.each do |leading, following|
puts "leading: #{leading}"
puts "following: #{following}"
end

What is this?

This benchmark establishes baseline the cost-of-doing-business for using a given interpreted language. Each program is the nearest equivalent of a single exit(0) command in the given language. It establishes the lower threshold for initialization/teardown overhead before/after the interpreter can actually do any work.

The below tests were all run on the same CPU running the noop programs for 200 iterations. The result is the total amount of time in seconds it took to complete all iterations. Past results cannot be compared to newer results as they are usually run on different hardware.

Why?

for(i, 0, 100,
fizzed_or_buzzed := false;
if(i % 5 == 0, "Fizz" print; fizzed_or_buzzed := true);
if(i % 3 == 0, "Buzz" print; fizzed_or_buzzed := true);
if(fizzed_or_buzzed, "", i) println
)
class Foo
def initialize stuff
@stuff = stuff
end
def display
puts @stuff
end
end
@acook
acook / item.rb
Last active December 22, 2015 06:38
item selling example for freenode#ruby
class Item # item for sale
BASE_SALES_TAX = 0.10
def initialize sku, value, special_tax = 0
@sku = sku
@value = value
@special_tax = special_tax
end
def total
def clean_round value
rounded = value.round(2)
value == rounded ? value.round(0) : rounded
end
clean_round 10.0/3 #=> 3.33
clean_round 9.0/3 #=> 3
interp = 'blah blah'
text = "something \n something #{interp}"
puts text #=> "something \n something blah blah"
text = 'something \n something #{interp}'
puts text #=> "something \\n something \#{interp}"
text = %Q{something \n something #{interp}}
puts text #=> "something \n something blah blah"