View extract_log.rb
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
# WARNING: lots of edge-cases are not properly handled. this is just code I wrote to inspect some | |
# rails logs by hand in pry where I could easily deal manually in edge-cases. | |
# however, feel free to fork and fix the edge-cases (and tell me about it!) | |
# | |
# use like this: | |
# start_time = Time.local(2017,1,1) | |
# end_time = Time.local(2017,1,2) | |
# puts rails_log_within("path/to/logfile.log", start_time, end_time) | |
require "time" # Time.iso8601 is stdlib, not core |
View binary_tree.rb
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 Node | |
attr_accessor :data, :left, :right | |
def initialize(data) | |
@left = nil | |
@right = nil | |
@data = data | |
end | |
include Comparable |
View char_stats.rb
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
Stats = Struct.new(:strength, :dexterity, :endurance, :intelligence, :education, :social_status) do | |
def self.random(min: 5, max: 10) | |
new(*Array.new(6) { rand(min..max) }) | |
end | |
def +(other) | |
self.class.new( | |
strength+other.strength, | |
dexterity+other.dexterity, | |
endurance+other.endurance, |
View worker_queue.rb
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 "time" | |
require "pry" | |
require "thread" | |
class Logger | |
def initialize | |
@mutex = Mutex.new | |
end | |
def log_error(error) |
View Regexp__natural_numbers_up_to.rb
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 Regexp | |
def self.quantifier(min,max) | |
if !max | |
if min == 0 | |
"*" | |
elsif min == 1 | |
"+" | |
else | |
raise "Invalid" | |
end |
View ArgumentSplitter.js
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 ArgumentSplitter { | |
static split(argumentString) { | |
return (new this(argumentString)).parse().arguments | |
} | |
constructor(argumentString) { | |
this.argumentString = argumentString | |
this.arguments = null | |
} |
View ruby_pseudo_globals.c
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
// Ruby 2.3.1 | |
/* eval.c:1614: */ rb_define_virtual_variable("$@", errat_getter, errat_setter); | |
/* eval.c:1615: */ rb_define_virtual_variable("$!", errinfo_getter, 0); | |
/* io.c:12280: */ rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set); | |
/* load.c:1195: */ rb_define_virtual_variable("$\"", get_loaded_features, 0); | |
/* load.c:1196: */ rb_define_virtual_variable("$LOADED_FEATURES", get_loaded_features, 0); | |
/* process.c:7537: */ rb_define_virtual_variable("$?", rb_last_status_get, 0); | |
/* process.c:7538: */ rb_define_virtual_variable("$$", get_pid, 0); | |
/* re.c:3653: */ rb_define_virtual_variable("$~", match_getter, match_setter); | |
/* re.c:3654: */ rb_define_virtual_variable("$&", last_match_getter, 0); |
View temperature_expected.rb
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 Temperature | |
KelvinToCelsius = 273.15 | |
KelvinToFahrenheit = 459.67 | |
FahrenheitFactor = 5.0/9 | |
def self.from_kelvin(kelvin) # could just alias | |
new(k: kelvin) | |
end | |
def self.from_celsius(celsius) # IMO should be just named "celsius" |
View explanation.txt
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 is afaik mostly for assignment methods. e.g. `a.foo ||= b` expanding to `a.foo = a.foo || b` | |
would invoke foo= regardless of whether it's necessary. `a.foo || a.foo = b` would not. |
View Numeric_localize.rb
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 Numeric | |
# Localize a number, adding thousands and decimals separators and setting the | |
# number of decimals | |
# | |
# @example | |
# 1234.56.localize # => "1,234.56" | |
# 1234.56.localize(thousands: "'", decimal: ",", places: 4) # => "1'234,5600" | |
def localize(thousands: ',', decimal: '.', places: nil) | |
raise ArgumentError, "decimal must be set" unless decimal |
NewerOlder