Skip to content

Instantly share code, notes, and snippets.

View maxjacobson's full-sized avatar
🍫

Max Jacobson maxjacobson

🍫
View GitHub Profile
lvar = 1
defined?(lvar) # => "local-variable"
defined?(no_lvar)
$gvar = 1
defined?($gvar) # => "global-variable"
defined?($no_gvar) # => nil
CONST = 1
defined?(CONST) # => "constant"

Will:

#!/usr/bin/env ruby

class Fountain
  def self.x=(x)
    @x = x
  end
require "English"
def some_method
# I didn't know it would still be set here
$ERROR_INFO # => #<RuntimeError: hell>
end
begin
raise "hell"
rescue
is everything working?
no yes
you're done!
are your tests passing? great job! open a PR
yes no
write a failing test, make your tests pass,
go back to beginning go back to beginning
# usage:
# ruby trie.rb chatter
word = ARGV.first || ["chatter", "helio", "wonder", "gosli"].sample
class Node
def add(chars)
return if chars.empty?
char = chars.shift
require 'readline'
def reminder
puts "vi editing mode? #{Readline.vi_editing_mode?}"
puts "emacs editing mode? #{Readline.emacs_editing_mode?}"
puts "Try:
writing something!
going up and down in history
going forward and backwards some characters
@maxjacobson
maxjacobson / struct
Last active November 7, 2016 23:16
handy script to print out the structure of a json file
#!/usr/bin/env ruby
require 'json'
require 'yaml'
def simple_structure(orig)
case orig
when Hash
simple = {}
orig.each do |key, value|
trap('INT') do
print("\n")
exit
end
output = Enumerator.new do |yielder|
chars = "▁▂▃▄▅▆█".split("")
chars = chars + chars.reverse
nums = (0..(chars.length - 1)).to_a
loop do
require 'github_api' # gem install github_api
require 'readline'
require 'fileutils'
FileUtils.touch("./safe")
username = Readline.readline("Enter your github username: ")
pw = Readline.readline("Enter your regular github password: ")
one_time_password = Readline.readline("Enter your one time password: ")
@maxjacobson
maxjacobson / html_report.rb
Created January 22, 2016 03:29
This is an idea to help compare two HTML files which may have a lot of superficial differences but are structurally the same. Specifically, it's meant to reduce an HTML file to its essence... and reveal just the elements and classes
require 'nokogiri'
failing = Nokogiri::HTML(File.read(ARGV.first))
def node_to_just_classes(node)
{
'name' => node.name,
'classes' => (node.attributes["class"]&.value&.split(" ")&.compact || []),
'children' => node.children.map { |child| node_to_just_classes(child) }
}