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
# 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 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
// 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 | |
} |
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
# 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} |
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 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 |
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
"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 |
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
def bitmask | |
2 ** 32 - 1 | |
end | |
def bitmask_with_assignment | |
overshoot = 2 ** 32 | |
overshoot - 1 |
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 Symbol | |
def | other | |
-> arg { other.to_proc[arg.send(self)] } | |
end | |
def call(*args) | |
-> arg { arg.send(self, *args) } | |
end | |
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
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 |
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
# /app/components/link_as_button.rb | |
class LinkAsButton | |
attr_reader :href | |
attr_reader :disabled | |
def initialize( | |
href:, | |
disabled: false, | |
&blk, | |
) |