Skip to content

Instantly share code, notes, and snippets.

View JEG2's full-sized avatar

James Edward Gray II JEG2

View GitHub Profile
@JEG2
JEG2 / what_am_i.md
Created February 19, 2014 00:49
I'm trying to solve or at least learn more about this problem.

I have a problem that pretty much boils down to this:

a = [1, 2, 3]
b = [1.1, 2.2, 3.3]

# BEGIN GROSS SOLUTION:  avert your eyes!
results = [ ]
a.each do |i|
 b.each do |j|
@JEG2
JEG2 / parse.rb
Created March 6, 2014 19:30
An example of parsing CSV-like data using | characters as separators and keeping header support
headers = nil
File.foreach(path) do |line|
fields = line.split(“|”)
if headers
row = headers.zip(fields)
# …
else
headers = fields
end
end
@JEG2
JEG2 / rpn.rs
Created August 15, 2014 18:53
An early stab at writing some Rust. I have questions.
use std::fmt;
use std::os;
struct Stack {
numbers: Vec<f64>
}
impl Stack {
fn new() -> Stack {
Stack{numbers: vec![]}
}
@JEG2
JEG2 / lifetimes.rs
Created August 21, 2014 14:20
An exploration of the concept of lifetimes.
// I'm trying to make sure I have my head around "lifetimes"
// after a few readings of the guide:
// http://doc.rust-lang.org/guide-lifetimes.html. The following is
// a simple example of what I think I now know that I'll try to explain
// in this comment.
//
// Because the letters are "borrowed references," (Do I have my terms right?)
// they could potentially be freed before the Lists struct that contains them.
// Since this possibility exists, Rust requires the explicit lifetime 'a which
// indicates that the lifetime of a created Lists instance is tied to whatever
@JEG2
JEG2 / increment.rs
Created August 23, 2014 02:59
Incrementing a list of numbers without mut.
fn increment(numbers: &Vec<int>) -> Vec<int> {
numbers.iter().map(|n| n + 1).collect()
}
fn main() {
let before = vec![1i, 2, 3];
let after = increment(&before);
println!("{}", after);
}
@JEG2
JEG2 / iterators.rs
Created August 28, 2014 14:34
A type question abou Rust.
// The uncommented version of increment_all() works. What I'm trying to
// understand is why I can't handle the return type as shown in the commented
// out version, which does not compile.
// fn increment_all<I: Iterator<int>>(numbers: I) -> I /* or Iterator<int> */ {
fn increment_all<'r, I: Iterator<int>>(numbers: I) -> std::iter::Map<'r, int, int, I> {
numbers.map(|n| n + 1)
}
fn main() {
@JEG2
JEG2 / pathfinder.rs
Created August 29, 2014 14:47
Exploring capturing strings in a Rust task.
use std::collections::HashMap;
fn main() {
let mut services = HashMap::new();
services.insert("S1", vec!["A", "C"]);
for (name, stops) in services.iter() {
spawn( proc() {
println!("{}", name);
} );
@JEG2
JEG2 / surprise.rb
Created February 19, 2015 15:28
Is this a bug in Ruby or just a poor method definition on my part?
class HashLike
def to_hash
{a: 1, b: 2}
end
end
def surprise(*args, keyword: nil)
p args
end
@JEG2
JEG2 / retry.rb
Created February 24, 2015 17:37
Minitest stubbing is weird
require "minitest/autorun"
class Something
def dont_do_it
:oops
end
end
class RetryTest < Minitest::Test
def test_different_return_values
@JEG2
JEG2 / example.rb
Created May 29, 2015 19:37
A manual `gsub!()`?
>> s = "active_model/errors"
=> "active_model/errors"
>> i = 0
=> 0
>> while (md = s.match(/(?:_|(\/))([a-z\d]*)/i, i)); s[md.begin(0)...md.end(0)] = "#{md[1]}#{md[2].capitalize}"; i = md.end(0) - 1 end
=> nil
>> s
=> "activeModel/Errors"