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 | |
# If no local variable is found, | |
# it begins the method lookup that ends calling | |
# the method_missing chain until the last one raises the error | |
begin | |
p undefined_name if true | |
rescue | |
puts "Raised an error" |
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 MegaLotto | |
NUMBERS = 5 | |
def draw | |
Array.new(NUMBERS){ single_draw } | |
end | |
private | |
def single_draw |
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 | |
def the_main_script | |
# Your main script | |
# Do whatever here | |
x = rand(3) | |
puts "x = #{x}" | |
puts "and 10/#{x} = #{10/x}" # When x==0 ZeroDivisionError | |
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
require 'psych' | |
class ScalarHandler < Psych::Handler | |
def parser=(parser) | |
@parser=parser | |
end | |
def mark | |
@parser.mark |
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 append_data_to_daily_files | |
Dir.entries('A').each do |file| | |
next unless file.match(/today/i) | |
File.open(File.join("A",file), 'a') do |file| | |
file.puts("hello") | |
end | |
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
#!/usr/bin/env ruby | |
regexp = /^([A-Z]{1,4})([0-9]{1,4})$/ | |
text_phrases = | |
[ "ABCD1234", | |
"ABCDE1234", | |
"ABCD12345", | |
"ABCDE12345"] |
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
require 'benchmark' | |
n = 100_000 | |
ar = [4,5,6,4,5,6,6,7] | |
Benchmark.bm(15) do |b| | |
b.report("Ralph Shneiver:"){ n.times { result = Hash.new(0); ar.each { |x| result[x] += 1 }; result} } | |
b.report("Sigurd:") { n.times { ar.inject(Hash.new(0)) {|res, x| res[x] += 1; res } } } | |
b.report("Keinich #1") { n.times { Hash[ar.group_by{|n|n}.map{|k,v|[k, v.size]}] } } | |
b.report("Keinich #2") { n.times { Hash.new(0).tap{|h|ar.each{|n|h[n] += 1}} } } | |
b.report("Magnus Holm:") { n.times { ar.each_with_object(Hash.new(0)) { |x, res| res[x] += 1 } } } |
NewerOlder