Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
View paint
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'optionparser'
KEYWORDS = %i[color metadata glow_in_the_dark coats].freeze
DEFAULTS = {coats: 3, glow_in_the_dark: true}.freeze
View transducers.rb
map = -> f {
-> rf {
-> acc, elem {
rf[acc, f[elem]]
}
}
}
square = -> n { n**2 }
add = -> acc, n { acc + n }
@smarr
smarr / truffle-material.md
Last active Oct 14, 2020
Truffle: Languages and Material
View truffle-material.md
@jhass
jhass / RubyOnArch.md
Last active May 2, 2020
My Ruby setup on Archlinux
View RubyOnArch.md

Ruby on Archlinux

I thought I would document my setup, since it's somewhat non-standard but working quite well for me.

What this does

  • Install major Ruby versions at their latest patch release
  • Allow to switch between them seamlessly
  • Use chruby
  • Encourage bundler usage
View gvl_release.rb
require 'fiddle'
class GVL
handle = Fiddle::Handle::DEFAULT
address = handle['rb_thread_blocking_region']
func = Fiddle::Function.new address, [Fiddle::TYPE_VOIDP,
Fiddle::TYPE_VOIDP,
Fiddle::TYPE_VOIDP,
Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP
@havenwood
havenwood / bench.md
Last active Dec 12, 2015
Entirely Unscientific Benchmark of Primes in Various Ruby Implementations
View bench.md

Unscientific Benchmark

The Benchmark

def is_prime? n
  (2...n).all? { |i| n % i != 0 }
end

def sexy_primes n
View tmux_cheatsheet.markdown

tmux cheatsheet

As configured in my dotfiles.

start new:

tmux

start new with session name:

View gist:3436258

How to use Ruby like PHP

If you're like me, you often want to stick a script on your web server and have it run. For that, PHP's pretty much your only option... right?

Nope!

Here's a little trick I did today that I'd like to share. I'll assume you're running Apache.

Add the following to a .htaccess file in your document root:

@ordinaryzelig
ordinaryzelig / minitest_spec_expectations.md
Last active Sep 22, 2020
How to write MiniTest::Spec expectations
View minitest_spec_expectations.md

I'm a fan of MiniTest::Spec. It strikes a nice balance between the simplicity of TestUnit and the readable syntax of RSpec. When I first switched from RSpec to MiniTest::Spec, one thing I was worried I would miss was the ability to add matchers. (A note in terminology: "matchers" in MiniTest::Spec refer to something completely different than "matchers" in RSpec. I won't get into it, but from now on, let's use the proper term: "expectations").

Understanding MiniTest::Expectations

Let's take a look in the code (I'm specifically referring to the gem, not the standard library that's built into Ruby 1.9):

# minitest/spec.rb

module MiniTest::Expectations
@havenwood
havenwood / xday.rb
Created Dec 1, 2011
Yesterday, Today, Tomorrow
View xday.rb
#!/usr/bin/env ruby
class Time
WEEKDAYS = %w[ Sunday Monday Tuesday Wednesday Thursday Friday Saturday ]
def day_before_yesterday
xday -2
end
def yesterday
You can’t perform that action at this time.