Skip to content

Instantly share code, notes, and snippets.

@dalizard
dalizard / 00-ADVENT-OF-CODE-2022-RUBY.md
Created February 21, 2024 13:07 — forked from skanev/00-ADVENT-OF-CODE-2022-RUBY.md
Advent of Code 2022 in Ruby (sort of)

Advent of Code 2022 solutions in Perl Ruby

Just for kicks, I'm trying to solve them with semi-golfed Ruby. That is:

  • As short as possible, but
  • Have some whitespace to make them somewhat readable
  • Avoid single-letter identifiers to keep them somewhat readable
  • Don't make them too short when they are short enough

Each solution parses the input file and outputs the answer.

@dalizard
dalizard / http_debug.rb
Created February 15, 2024 12:23 — forked from dathanb/http_debug.rb
Print Raw HTTP Request and Response Payload in Ruby
module Net
class HTTP
alias_method(:orig_request, :request) unless method_defined?(:orig_request)
def request(req, body = nil, &block)
puts '===== REQUEST ====='
puts "Request: #{req.method} http://#{@address}:#{@port}#{req.path}"
puts 'Request headers: '
req.each_header do |key, value|
puts "\t#{key}: #{value}"
times in msec
clock self+sourced self: sourced script
clock elapsed: other lines
000.049 000.049: --- NVIM STARTING ---
000.929 000.880: locale set
001.421 000.492: inits 1
001.441 000.020: window checked
001.623 000.182: parsing arguments
001.884 000.261: expanding arguments
-module(frequency_tests).
-include_lib("eunit/include/eunit.hrl").
-define(setup(F), {setup, fun start/0, fun stop/1, F}).
%%%
%%% TESTS DESCRIPTIONS
%%%
boot_up_test_() ->
{"Can be started with a registered name",
%% Based on code from
%% Erlang Programming
%% Francecso Cesarini and Simon Thompson
%% O'Reilly, 2008
%% http://oreilly.com/catalog/9780596518189/
%% http://www.erlangprogramming.org/
%% (c) Francesco Cesarini and Simon Thompson
-module(frequency).
-export([init/0]).
@dalizard
dalizard / rules for good testing.md
Created November 18, 2016 09:19 — forked from Integralist/rules for good testing.md
Sandi Metz advice for writing tests

Rules for good testing

Look at the following image...

...it shows an object being tested.

You can't see inside the object. All you can do is send it messages. This is an important point to make because we should be "testing the interface, and NOT the implementation" - doing so will allow us to change the implementation without causing our tests to break.

diff -urN mutt-1.6.1/buffy.c mutt-1.6.1-sidebar/buffy.c
--- mutt-1.6.1/buffy.c 2016-05-02 03:02:12.397171385 +0100
+++ mutt-1.6.1-sidebar/buffy.c 2016-05-02 03:02:15.009212943 +0100
@@ -27,6 +27,10 @@
#include "mutt_curses.h"
+#ifdef USE_SIDEBAR
+#include "sidebar.h"
+#endif
[1] pry(main)> module A
[1] pry(main)* module B; end
[1] pry(main)* end
=> nil
[2] pry(main)> A::B.class_eval { Module.nesting }
=> []
[3] pry(main)> A::B.class_eval('Module.nesting')
=> [A::B]
[4] pry(main)>
Destination Gateway Flags Refs Use Netif Expire
default 10.205.1.1 UGSc 17 43 en0
10.205.1/24 link#4 UCS 4 0 en0
10.205.1.1 bc:16:65:93:6a:93 UHLWIir 18 112 en0 1190
10.205.1.13 127.0.0.1 UHS 0 0 lo0
10.205.1.18 c8:a:a9:4a:62:80 UHLWIi 0 45 en0 1164
10.205.1.21 1c:75:8:c3:20:ab UHLWIi 3 2132 en0 1168
10.205.1.23 74:e5:b:d:e4:20 UHLWIi 0 0 en0 1109
127 127.0.0.1 UCS 0 0 lo0
127.0.0.1 127.0.0.1 UH 2 10146422 lo0
Top 50 slowest examples (299.23 seconds, 12.7% of total time):
SearchResult.strip_user_from_search_results removes users from search results, who have been banned by the user
8.73 seconds ./spec/models/search/search_result_spec.rb:55
Omniture tracking (set stat_secret.var_name variables) Profile visited Should set "prop17" to visited profile info (age1|[age2]#gender1|[gender2]#pic?#FFT|SFT)
and "prop5" to pageName_visitedProfileInfo
when visiting own profile (page my_profile_common_show)
8.56 seconds ./spec/features/omniture_tracker_spec.rb:444
SearchResult.strip_user_from_search_results does nothing when there are no banned relations
8.13 seconds ./spec/models/search/search_result_spec.rb:67
SearchResult.strip_user_from_search_results removes users from search results, who have banned the user