Skip to content

Instantly share code, notes, and snippets.

# Mixin to scan an array for objects that respond to a given method
module WithMethod
# Iterates through the array and returns a list of items that
# respond to method
def with_method(method)
self.select do |item|
item.respond_to(method)
end
end
// This would be sweet Miko:
// Go Object.property!
Speck.describe("your mother") {
it.should("be true") {
true.should = true
}
it.should("not be false") {
false.should_not = true
}
# Creates a little histogram of the frequency of words occuring in your string
# Usage : ruby word_histogram.rb "Test string, string"
h=Hash.new(m=0);ARGV[m].scan(/\w+/).map{|x|h[x.downcase]+=1;m=[m,x.size].max};h.sort.map{|k,v|puts"=> %-#{m+4}s#{'*'*v}"%k}
@judofyr
judofyr / echo.rb
Created November 21, 2009 15:46 — forked from sarahhodne/echo.rb
class Echo < Plugin
author "Henrik Hodne (dvyjones)"
permission :echo, :default => false
help "Says whatever text you pass to it."
def echo(line)
say(line)
end
end
"SortedSet" behaves like "Array (sorted after every insert)" and "(insert in place)".
Rehearsal ---------------------------------------------------------------------
Array (unsorted) 0.120000 0.000000 0.120000 ( 0.120734)
Set (unsorted) 0.000000 0.000000 0.000000 ( 0.000302)
Array (sorted once) 0.130000 0.000000 0.130000 ( 0.121996)
Array (sorted after every insert) 0.350000 0.000000 0.350000 ( 0.355280)
SortedSet 0.120000 0.030000 0.150000 ( 0.151816)
Array (insert in place) 0.090000 0.000000 0.090000 ( 0.093471)
------------------------------------------------------------ total: 0.840000sec
#!/usr/bin/env ruby
require 'benchmark'
def bitmask
2 ** 32 - 1
end
def bitmask_with_assignment
overshoot = 2 ** 32
overshoot - 1
@judofyr
judofyr / fun.rb
Created August 1, 2012 14:25 — forked from andkerosine/fun.rb
class Symbol
def | other
-> arg { other.to_proc[arg.send(self)] }
end
def call(*args)
-> arg { arg.send(self, *args) }
end
end
@judofyr
judofyr / fizzbuzz.rb
Created August 1, 2012 21:37 — forked from JEG2/fizzbuzz.rb
Writing FizzBuzz with flip-flops
a=b=c=(1..100).each do |num|
print num, ?\r,
("Fizz" unless (a = !a) .. (a = !a)),
("Buzz" unless (b = !b) ... !((c = !c) .. (c = !c))),
?\n
end
# /app/components/link_as_button.rb
class LinkAsButton
attr_reader :href
attr_reader :disabled
def initialize(
href:,
disabled: false,
&blk,
)