Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
willmurphyscode / destructure-copyable-vs-uncopyable.rs
Created July 10, 2017 11:09
This is shows the difference in Move behavior between a boxed struct whose members can be copied and a box struct whose members can't be copied.
// THANKS TO https://rustbyexample.com/std/box.html
// for the code that was modified into this example
struct Point {
x: f64,
y: f64,
}
struct Rectangle {
p1: Point,
extern crate rand;
mod node;
mod helpers;
fn recursive_count_fruit(node: Option<Box<node::Node>>) -> i32 {
match node {
Some(val) => {
let our_fruit = if val.has_fruit { 1 } else { 0 };
let unboxed = *val;
@willmurphyscode
willmurphyscode / node.rs
Created June 24, 2017 13:30
Simple node struct for making a tree
use helpers;
use std::fmt;
#[derive(Debug)]
pub struct Node {
pub left_child: Option<Box<Node>>,
pub right_child: Option<Box<Node>>,
pub has_fruit: bool,
}
@willmurphyscode
willmurphyscode / hash_on_mutated_array.rb
Created March 26, 2017 22:08
Demonstration that mutating an array in Ruby changes it's hash value
def does_it_change
mutable_array = [1,2]
hash = {}
hash[mutable_array] = 'foo'
puts hash[mutable_array].inspect # prints "foo"
mutable_array[1] = 3
puts hash[mutable_array].inspect # prints "nil"
end
does_it_change
@willmurphyscode
willmurphyscode / HashOnMutatedArray.cs
Last active March 26, 2017 22:37
Simple demonstration that mutating an array in C# doesn't change its hash function, in contrast with Ruby.
using System;
using System.Collections.Generic;
namespace array_compare
{
class Program
{
static void DoesItChange()
{
Dictionary<int[], string> hash = new Dictionary<int[], string>();
class Stubbable
def stub(name, block)
send(:define_singleton_method, name, block)
end
end
class Class
def stub(name, block)
send(:define_singleton_method, name, block)
end
@willmurphyscode
willmurphyscode / csharp_style.rb
Created March 6, 2017 12:06
An object that intercepts `method_messing` so that it appears you can call class
class CSharpStyle
def method_missing method_name, *args
self.class.send(method_name, *args)
end
def self.static_method
puts "hurray for Ruby"
end
def instance_method
using System;
namespace ConsoleApplication
{
public class HasInstanceAndStaticMethods
{
public static void StaticMethod()
{
Console.WriteLine("Hello from static method");
}
@willmurphyscode
willmurphyscode / instance_and_class.rb
Created March 6, 2017 11:57
Error calling class methods from instance methods in Ruby
class HasInstanceMethods
def instance_method
puts "In instance method, the type of `self` is #{self.class}"
end
def self.class_method
puts "In class method, the type of `self` is #{self.class}"
end
def call_class_from_instance
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"