Skip to content

Instantly share code, notes, and snippets.

View domgetter's full-sized avatar

Dominic Muller domgetter

View GitHub Profile
class PokerHandRank
RANKS = %w(high pair tpair trip stright flush full four royal)
def initialize(hand)
@hand = hand
end
def flush?
@hand.suits.any? {|k, v| v >= 5 }
names = Car.uniq.pluck(:name)
wheel_counts = Car.uniq.pluck(:wheels)
groups = []
names.each do |name|
wheel_counts.each do |wheel_count|
groups << Car.where(name: name, wheel: wheel_count)
end
end
> vi spec/controllers/users_controller_spec.rb
> rspec spec/controllers/users_controller_spec.rb
UsersController
GET 'index'
returns http success
GET 'show'
returns http fail (FAILED - 1)
GET 'new'
returns http success
GET 'edit'
returns http success (FAILED - 2)
require 'rails_helper'
RSpec.describe UsersController, :type => :controller do
describe "GET 'index'" do
it "returns http success" do
get 'index'
expect(response).to be_success
end
end
XXXXXXXX
X X
X XXXX X
X X X X
X X X X
X XXXX X
X X
XXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
# here's what Array would output from inspect if it wasn't overridden:
class Array
def inspect
"<##{self.class}:" + "0" + "x" + "#{self.object_id.to_s(16)}>"
end
end
[1,2,3].inspect
=> "<#Array:0x1491570>"
@domgetter
domgetter / ffi_callback_segfault.rb
Created July 28, 2014 09:29
Segmentation Fault example from waveOutProc callback for waveOutOpen windows multimedia function.
require 'ffi'
class HWAVEOUT < FFI::Struct
layout :i, :int
end
class WAVEFORMATEX < FFI::Struct
def initialize
self[:wFormatTag] = 1
self[:nChannels] = 1
@domgetter
domgetter / segfault_console_output
Created July 28, 2014 09:34
ffi callback segfault console output
waveOutOpen returns 0 (no error) if I don't use the callback form
0
but if I use a callback, the moment it returns from the callback, there is a seg fault
note that the code inside the callback runs, as I am puts-ing the mMsg which is 955
for WOM_OPEN which tells us the device was opened.
this is on ruby 1.9.3p484 (2013-11-22) [i386-mingw32] running 1.9.3 x86-mingw32 on
Windows 7
955
sound.rb:67: [BUG] Segmentation fault
ruby 1.9.3p484 (2013-11-22) [i386-mingw32]
@domgetter
domgetter / IEEE754.rb
Last active August 29, 2015 14:04
convert float to bits
bits = eval("0x" + [0.15625].pack('g').unpack('H16')[0]).to_s(2).rjust(32,"0")
#=> "00111110001000000000000000000000"
# these are the bits of 0.15625 represented as a single precision float
sign = bits[0]
#=> "0"
exponent = bits[1..8]
#=> "01111100"
mantissa = bits[9..32]
#=> "01000000000000000000000"
sign = sign.to_i == 0 ? 1 : -1