Skip to content

Instantly share code, notes, and snippets.

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"
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
@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
class Foo
def initialize stuff
@stuff = stuff
end
def display
puts @stuff
end
end
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
)

What is this?

This benchmark establishes baseline the cost-of-doing-business for using a given interpreted language. It gauges the initialization/teardown overhead before/after the interpreter can actually do any work. Each program is the nearest equivalent of a single exit(0) command as possible. This is not a general-purpose benchmark, but it's interesting to see how different interpreters behave. The below tests were all run on the same CPU running the noop programs for 200 iterations.

Why?

The original impetus was for a project called impel which was going to be executed in the $PS1, $PROMPT_COMMAND, precmd, or equivalent where I wanted to avoid adding latency to the command prompt but was ambivalent about the language it was written in. There are certainly other concerns to be had beyond this overhead, it's just one of many things to take into consideration.

letter_pairs = [%w{a b}, %w{c d}]
letter_pairs.each do |leading, following|
puts "leading: #{leading}"
puts "following: #{following}"
end
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
@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 AuthEngine < Sinatra::Base
set :sessions => true
register do
def auth type
condition do
redirect "/login" unless send "is_#{type}?"
end
end