Skip to content

Instantly share code, notes, and snippets.

View domgetter's full-sized avatar

Dominic Muller domgetter

View GitHub Profile
@domgetter
domgetter / description.txt
Last active August 29, 2015 14:04
Interdefined Bindata Records problem
I want to create two records which refer to each other, but
Bindata seems to not want to let me do that.
The two solutions I came up with are to create a copy of one
record to another class with most the same functionality, or
to create an anonymous struct in one to copy most the functionality.
Both of these solutions require me to repeat myself, so I'd
like to create a more compact version like the first example.
Am I defining things wrong or does Bindata simply not allow
@domgetter
domgetter / cool_enum.rb
Created August 2, 2014 19:27
enum with select to return custom collection
class CoolCollection
include Enumerable
def initialize
@elements = []
end
def <<(elem)
@elements << elem
end
def each &block
@domgetter
domgetter / its_example_group.rb
Created August 4, 2014 15:32
its as an example group in rspec
describe Pitch
let(:pitch) {Pitch.new}
describe "#+" do
context "when you add 1 to a pitch" do
its "frequency is bigger" do
expect((pitch + 1).frequency).to be > pitch.frequency
end
end
end
end
class Dog
end
dog = Dog.new
eigen = class << dog
self
end
eigen.superclass
#=> Dog
class Object
def eigen
str = caller[0]
/\w+(?=')/ =~ str
singleton_class.send $&.to_sym
end
end
class Animal
def speak
//Java syntax
timer.runEverySecond2((int x) -> {
System.out.println("Hello, world! Your # is: " + x);
});
//Ruby syntax
timer.runEverySecond2(->(x) {
$stdout.print("Hello, world! Your # is: " + x.to_s);
});
require 'sound'
Sound::Device.new {|d| d.play Sound::Data.new.sine_wave(440, 500, 1)}
# This is the short way that opens and closes the device automatically
# Here is the more verbose way:
device = Sound::Device.new("w")
format = Sound::Format.new(Sound::Format::PCM)
data = Sound::Data.new(format)
@domgetter
domgetter / attr_reader_issue.rb
Created August 12, 2014 05:23
How do I prevent the @children array from being changed outside the class since I only used attr_reader ?
class Human
attr_reader :children
def initialize
@children = ["Joe", "Henry"]
end
end
#=> nil
human = Human.new
#=> #<Human:0x2a19bd8 @children=["Joe", "Henry"]>
human.children << "Mary"
@domgetter
domgetter / code_sharing.rb
Last active August 29, 2015 14:05
How to share methods between modules while maintaining best namespacing practices.
module GemName
module CoolClassInterface
module Win32
def func_a; end
def func_b; end
end
module Linux
def func_a; end # but now I don't have DRY code. How do I make Win32 and Linux share this?
def func_c; end
end
threads = []
threads << Thread.new do
Thread.stop
puts "(first) I started up again!"
end
threads << Thread.new do
Thread.stop
puts "(second) I started up again!"