View pry_prompt.rb
# Based on this: https://github.com/pry/pry/blob/782a09e057d1e714074d61891a5fba293eb9faca/lib/pry.rb#L54-L63
# And intended to reproduce the prompt in this: https://vimeo.com/26391171
Pry.config.prompt = [
proc { |target_self, nest_level, pry|
"\e[45m#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}"}>\e[0m "
},
proc { |target_self, nest_level, pry|
"\e[45m#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}"}*\e[0m "
}
]
View immutability_and_modifying_integers.rb
RUBY_VERSION # => "2.5.0"
class Integer
def add! n
# don't do this in real code, it's a timebomb full of segfault
require 'fiddle'
digits = Fiddle::Pointer.new object_id*2+16
digits[0, 8] = [digits[0, 8].unpack('Q')[0]+n].pack('Q')
end
end
View PE100.java
import static java.lang.Math.sqrt;
import java.math.BigInteger; // https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
public class PE100 {
static BigInteger zero = new BigInteger("0");
static BigInteger one = new BigInteger("1");
static BigInteger two = new BigInteger("2");
static BigInteger four = new BigInteger("4");
static BigInteger eight = new BigInteger("8");
static BigInteger sixteen = new BigInteger("16");
View pe100.rb
def show(r, s_root)
b = (2*r + 1 + s_root)/2
p b: b, r: r, a: b+r
end
# calculate the first r root
a = 10**12 # => 1000000000000
b = a/Math.sqrt(2) # => 707106781186.5475
r_root = (a-b).to_i # => 292893218813
r_root -= 1 if r_root.even?
View structure_with_heather.rb
def print_row(row)
guess = row[:guess]
print "|#{guess[0]}|#{guess[1]}|#{guess[2]}|#{guess[3]}| |\n"
end
def print_board(board)
print "---------------\n"
board[:rows].each do |row|
print_row(row)
end
View implementing_sorbet_with_tracepoint_exampe.rb
class Sig
INSTANCE = new
def declared(mod, param_types)
@current = [mod, param_types]
self
end
def returns(return_type)
@current << return_type if @current
View example_of_how_to_compare_two_classes_to_make_sure_their_interfaces_match.rb
class LHS
def correct() end
def wrong_num_args(a) end
def wrong_arg_types(a,b:) end
def wrong_name(a) end
def lhs_only() end
end
class RHS
def correct() end
View header_hierarchy_problem.rb
# The goal of this problem is to extract headers from a block of text,
# and arrange them hierarchically.
#
# See the specs for more detail on the output
require 'net/http'
require 'nokogiri'
def header_hierarchy(html)
# https://www.w3.org/MarkUp/html3/headings.html
View implementing_sorbet_example.rb
# An implementation to make this code example work: https://tinyurl.com/y7tzjutf
class Sig
def initialize(param_types)
@param_types = param_types
end
def returns(return_type)
@return_type = return_type
self
View a_bunch_of_ways_to_get_a_pristine_binding_for_an_object.rb
def get_binding_via_implicit_block
yield.instance_eval { binding }
end
b = get_binding_via_implicit_block { 111 }
b.receiver # => 111
b.local_variables # => []
def get_binding_via_ivar