Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / extract_log.rb
Last active Feb 16, 2018
Extract pieces from line-based logfiles
View extract_log.rb
# 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 Feb 8, 2018 — forked from CodePint/binary_tree.rb
binary tree and nodes
View binary_tree.rb
class Node
attr_accessor :data, :left, :right
def initialize(data)
@left = nil
@right = nil
@data = data
end
include Comparable
View char_stats.rb
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
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
class Regexp
def self.quantifier(min,max)
if !max
if min == 0
"*"
elsif min == 1
"+"
else
raise "Invalid"
end
View ArgumentSplitter.js
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 Sep 27, 2016
Globals in ruby which really are native C functions pretending to be globals.
View ruby_pseudo_globals.c
// 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
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 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 Nov 5, 2015
localize a number (thousands and decimal separator)
View Numeric_localize.rb
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
You can’t perform that action at this time.