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 foo | |
raise "I iz a fool" | |
"But you don't know, man" | |
rescue Exception => e | |
"I caught your foolness" | |
ensure | |
"and I returned you a fish" | |
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
### | |
Randomize array element order in-place. | |
Using Fisher-Yates shuffle algorithm. | |
### | |
Array.prototype.shuffle = -> | |
for i in [(@length - 1) .. 0] | |
j = Math.floor(Math.random() * (i + 1)) | |
[@[i], @[j]] = [@[j], @[i]] | |
@ |
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
# My answer at the time was to do something like this | |
[[nil]*3]*4 | |
#=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] | |
# The next question was whether I knew some initializer to do that for us. | |
# Apparently, there is: | |
Array.new(4) { Array.new(3) } | |
#=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]] | |
# But that wasn't the brainwave my brain came up with just now. |
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 a(p) | |
p.call() | |
raise "The world is safe" | |
end | |
def b | |
x = Proc.new { return Proc.new { return false } } | |
a(x).call | |
raise 'Maybe here, we might save the world. Let us do that.' | |
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
#!/usr/bin/env ruby | |
require 'fileutils' | |
def shell(cmd) | |
puts "Running #{cmd}" | |
`#{cmd}` | |
end | |
infile = ARGV[0] |
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 Kitten | |
def initialize | |
set_colour | |
set_name | |
end | |
def set_colour | |
if rand(0..1) == 1 | |
@colour = 'White' |
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 'net/http' | |
URL_LIST = [ | |
[:get, 'http://quickwebdesign.net'], | |
[:post, 'http://retrodude.com'] | |
].map { |action, url| [action, URI.parse(url)] } | |
POST_DATA = -> do | |
{ :name => "foo", :what => 'ho', :interesting => rand(999999) } |
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 simple_benchmarker | |
benchmarks = [] | |
activity = ->(name) { benchmarks << [name, Time.now] } | |
activity[:started] | |
yield activity | |
activity[:finished] | |
display_benchmarks benchmarks | |
rescue Exception => e | |
activity[:error_occurred] | |
display_benchmarks benchmarks |
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
# Premise: a chainable DSL where evaluating a section in an if statement calls valid? | |
# Example: Each item changes the current synonym, it tests it against | |
class Moggy | |
SYNONYMS = [:cat, :moggy, :pussycat, :feline] | |
def set(syn) | |
@synonym = syn | |
self | |
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
"fish" =~ /ish/ | |
"fish" !=~ /ish/ | |
!("fish" =~ /ish/) # equivalent to "fish" !~ /ish/ - thanks @MrJaba and @kerryb! |
NewerOlder