This file contains 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
/// This is the opcode of the instruction. | |
enum Op { | |
Add, | |
Sub, | |
Mov, | |
} | |
/// This is a reference to a register. It is passed around when generating the | |
/// IR through the Assembler struct. When it gets loaded into an Insn struct as | |
/// part of a generated instruction, it is wrapped into an Opnd::Reg. |
This file contains 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 "json" | |
struct = { "a" => 1, "b" => 2, "c" => [1, 2, 3], "d" => [{ "e" => 3 }, nil, false, true, [], {}] } | |
source = JSON.dump(struct) | |
tokens = [] | |
index = 0 | |
until source.empty? | |
tokens << |
This file contains 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
#!/usr/bin/env ruby | |
# The purpose of this script is to extract all constant references from a given | |
# source file. It reads ARGF, which means you can either pass the content in | |
# through stdin or you can pass the file name as an argument. So for example, | |
# you can either: | |
# | |
# echo 'Foo::Bar' | ./constants | |
# | |
# or you can pass a file name, as in: |
This file contains 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
# frozen_string_literal: true | |
require "rubygems/package" | |
require "net/http" | |
require "tmpdir" | |
queue = Queue.new | |
Gem::SpecFetcher.new.available_specs(:latest).first.each do |source, gems| | |
gems.each do |tuple| | |
gem_name = File.basename(tuple.spec_name, ".gemspec") |
This file contains 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
# This class is responsible for publishing a post. This is basically a curried | |
# global function that is addressable through the PublishPost class. It can be | |
# easily tested, since its only dependency is the post that it accepts. | |
class PublishPost | |
attr_reader :post | |
def initialize(post) | |
@post = post | |
end |
This file contains 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 "minitest/autorun" | |
class TemplatingTest < Minitest::Test | |
def test_positional_argument_splats | |
assert_templates <<~RUBY, "h1", "*args" | |
@_target << "<h1>" << _output(*args) << "</h1>" | |
RUBY | |
end | |
def test_multiple_positional_argument_splats |
This file contains 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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "syntax_tree" | |
end |
This file contains 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
# This module is a light implementation of pattern matching that allows us to | |
# match against hashes and arrays as we would with the new pattern matching | |
# syntax. | |
module Pattern | |
class ArrayPattern | |
attr_reader :values | |
def initialize(values) | |
@values = values | |
end |
This file contains 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
# Imagine we need to switch on some kind of input and do something based on some | |
# attribute of that input. We can do this with a case statement, where we inline | |
# each of the implementations of the interface that we're defining into the case | |
# statement. That would look something like: | |
class Input | |
attr_reader :type, :name | |
def initialize(type, name) | |
@type = type |