Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / extract_log.rb
Last active February 16, 2018 15:41
Extract pieces from line-based logfiles
# 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
@apeiros
apeiros / binary_tree.rb
Last active February 8, 2018 16:10 — forked from CodePint/binary_tree.rb
binary tree and nodes
class Node
attr_accessor :data, :left, :right
def initialize(data)
@left = nil
@right = nil
@data = data
end
include Comparable
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,
require "time"
require "pry"
require "thread"
class Logger
def initialize
@mutex = Mutex.new
end
def log_error(error)
class Regexp
def self.quantifier(min,max)
if !max
if min == 0
"*"
elsif min == 1
"+"
else
raise "Invalid"
end
class ArgumentSplitter {
static split(argumentString) {
return (new this(argumentString)).parse().arguments
}
constructor(argumentString) {
this.argumentString = argumentString
this.arguments = null
}
@apeiros
apeiros / ruby_pseudo_globals.c
Last active September 27, 2016 13:03
Globals in ruby which really are native C functions pretending to be globals.
// 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);
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"
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.
@apeiros
apeiros / Numeric_localize.rb
Last active November 5, 2015 19:55
localize a number (thousands and decimal separator)
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