This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://babeljs.io/blog/2018/07/19/whats-happening-with-the-pipeline-proposal | |
| module Pipeable | |
| def |(fn) | |
| fn.to_proc.call self | |
| end | |
| refine(Enumerable) { include Pipeable } | |
| refine(Proc) { include Pipeable } | |
| refine(Array) { include Pipeable } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| p RUBY_VERSION: RUBY_VERSION | |
| def ab(a, b:) | |
| [a, {b: b}] | |
| end | |
| puts "Three dots:" | |
| def dots(...) ab(...) end | |
| pp real_sig: method(:ab).parameters, | |
| wrapper_sig: method(:dots).parameters, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // code for the screenshot in this tweet https://twitter.com/josh_cheek/status/1408853467150663680 | |
| #include "stdio.h" | |
| void inc1(int& n) { ++ n; } | |
| void inc2(int* n) { ++*n; } | |
| int main(int argc, char** argv) { | |
| int a = 0, b=0; | |
| printf("a=%d, b=%d\n", a, b); | |
| inc1(a); inc2(&b); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # An example to help me explain my thoughts in this tittwer convo: | |
| # https://twitter.com/dorianmariefr/status/1407972746093641732 | |
| # | |
| # Note that I ran this code on MRI 3.0.1, I assume it works on other versions too, but I did not try it. | |
| # ----------------------------------------------------------------- | |
| # For simplicity, we'll only deal with keyword args in this example, | |
| # I initially wrote it to deal with ordinals and blocks, but that got pointlessly dense |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Context: https://twitter.com/josh_cheek/status/1406730934880198664 | |
| # NOTE: If you want to run this in bash, you have tod ouble-escape the newlines being passed to `tr` | |
| curl -sL http://yaml.org/type/bool.html | # grab the docs on booleans | |
| nokogiri -e 'puts $_.css("pre.screen").text' | # extract the values they mention | |
| tr -d \n | # remove their whitespace formatting | |
| tr \| \n | # each BNF "or" option gets its own line | |
| ruby -r yaml -lne 'p [$_, YAML.load($_)]' | # print each input value and what it parses to | |
| bat -l ruby # syntax highlight it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # I used this to generate the public / private keypair and sign them | |
| require 'base64' | |
| require 'openssl' | |
| private_key = OpenSSL::PKey::RSA.generate 1024 | |
| public_key = private_key.public_key | |
| signature = private_key.sign | |
| OpenSSL::Digest::SHA256.new, | |
| public_key.to_pem | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://sites.google.com/site/steveyegge2/five-essential-phone-screen-questions | |
| # Area Number One: Coding | |
| # 1. Write a function to reverse a string. | |
| echo -e a\\nab\\nabc\\nabcdef© | ruby -lne 'puts $_.reverse' | |
| # 2. Write function to compute Nth fibonacci number | |
| seq 0 10 | ruby -ne ' | |
| a, b = 0, 1 | |
| $_.to_i.times { a, b = a+b, a } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # alternative solutions to https://blog.arkency.com/semantic-blind-spot-in-ruby-case-statement/ | |
| class CaseWhen | |
| def call(number) | |
| boundaries = [0, 4, 8, 11] | |
| messages = ['low value', 'medium value', 'high value'] | |
| boundaries.each_cons(2).zip messages do |(low, high), desc| | |
| return desc if low <= number && number < high | |
| end | |
| 'invalid value' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'objspace' | |
| require 'fiddle' | |
| require 'json' | |
| def heap | |
| $heap ||= ObjectSpace | |
| .dump_all(output: :string) | |
| .lines.map { JSON.parse _1, symbolize_names: true } | |
| .reject { |obj| %w[ROOT IMEMO DATA].include? obj[:type] } # probably overzealous | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| echo Josh | ruby -r ./tracer.rb -e ' | |
| class User | |
| def initialize(id, name) | |
| @id, @name = id, name | |
| end | |
| def to_s | |
| "User #@id: #@name" | |
| end | |
| end |