Skip to content

Instantly share code, notes, and snippets.

OPERATORS = ['+', '-']
ROUNDS = 2
MAX_NUMBER = 9
puts 'What is your name?'
name = gets.chomp
puts "Welcome to Arithmetic Tutor, #{name}!"
results = (1..ROUNDS).map do
left = rand(MAX_NUMBER + 1)
@tomdalling
tomdalling / PlayerController_States.h
Created March 20, 2013 02:01
Some code from a state machine for Oddworld-style platform movement.
//
// Copyright 2012. All rights reserved.
//
static const int StandingHeight = 2;
static const int CrouchingHeight = 1;
class State {
public:
@tomdalling
tomdalling / app_cell.rb
Last active April 11, 2016 07:28
Cells Erbse automatic escaping
require 'cell'
class AppCell < Cell::ViewModel
include ::Cell::Erb
def show
render
end
def template_options_for(*args)
@tomdalling
tomdalling / rschema_pluggable_coercion.rb
Last active May 7, 2017 16:07
Pluggable/composable coercion upcoming in RSchema v3
# params usually come in as strings, regardless of the actual type they are
params = {
'fruit' => 'banana',
'harvested_at' => '2017-05-08T01:46:16+10:00',
'unwanted_framework_crap' => 'DHH',
}
# you can declare what the types are _supposed_ to be with a schema
schema = RSchema.define {{
fruit: _Symbol,
@tomdalling
tomdalling / benchmarks.txt
Created June 13, 2017 00:07
Benchmarking input validation
Using Ruby 2.4.1
Code being benchmarked: https://github.com/tomdalling/rschema/blob/62da91a8c6e9b4795affe6824e23f27e713a1214/benchmarks/input_validation.rb
Warming up --------------------------------------
RSchema 3.1.1 1.640k i/100ms
(coerced) 673.000 i/100ms
(built + coerced) 142.000 i/100ms
ActiveModel 5.1.1 103.000 i/100ms
dry-validation 0.10.7
759.000 i/100ms
@tomdalling
tomdalling / notes.md
Created March 13, 2018 08:09
value object stuff

I like the idea of providing an RSpec shared example to test all these behaviours.

For the constructor, I would change "consider using keywords" to "should use keywords". Keywords should be the default, unless the order is really obvious.

Re: "Value construction options could be provided by keyword arguments." I would probably prefer value contruction options to be in separate class methods, instead of #new.

Re: caching. I would be hesitant to do this at the global level (e.g. stored in the class).

@tomdalling
tomdalling / article.md
Last active April 18, 2018 13:06
jekyll-include_snippet gem usage behind article: https://www.rubypigeon.com/posts/super-secret-methods/
title layout date snippet_source
Super Secret Methods
post
2017-07-16
code/super_secret_methods.rb

Here is a quirk of the Ruby language that I discovered a few weeks ago.

Method names can not contain a period character.

@tomdalling
tomdalling / rspec_output.md
Created January 20, 2019 02:32
Mutant RSpec error output

When I do puts method_name inside #respond_to_missing?, it outputs begin, so it looks like it's treating self as if it were a Range.

This is the RSpec output:

An error occurred while loading ./spec/value_semantics_spec.rb.
Failure/Error: first_letter = method_name[self]

SystemStackError:
  stack level too deep
@tomdalling
tomdalling / guide.md
Last active February 3, 2019 01:54
Tom's Narrow Guide to Melbourne

Tom's Narrow Guide to Melbourne

Places I like, for RubyConf AU attendees.

Coffee + Brunch Spots

  • Queen Victoria Markets
  • Degraves St
  • St Ali (South Melbourne, just outside the CBD. Must try the bottled iced lattes.)
  • Auction Rooms (North Melbourne, just outside the CBD)
@tomdalling
tomdalling / 1_without_details.txt
Last active August 8, 2020 02:12
TestBench output
Running test/automated/zettel/hashtag_query.rb
Zettel::HashtagQuery
matches the presence of a hashtag
with syntax: #a
provides logical NOT
with syntax: NOT #a
with syntax: not #a
with syntax: !#a
provides logical AND
with syntax: #a AND #b