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 / 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 / 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 / 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 / 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 / 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 / thread_log_test.txt
Created August 22, 2013 19:07
Showing Josh how MRI works.
~/Desktop [ruby 2.0.0p247]$ cat dont_close_my_logger.rb
require "logger"
log = ARGV.shift or abort "USAGE: #{$PROGRAM_NAME} LOG"
logger = Logger.new(log)
threads = [ ]
threads << Thread.new do
logger.info "Thread one started. Sleeping a bit to make sure thread two is going..."
sleep 1
@JEG2
JEG2 / arguments.rb
Created July 29, 2013 19:53
Uh, yuck.
class RequiredArgument
def initialize(one, keyword: false)
p [one, keyword]
end
end
RequiredArgument.new(Hash.new)
# ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]
# bug.rb:2:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
@JEG2
JEG2 / gist:6093417
Created July 27, 2013 02:30
Testing public pastes.
(set-face-attribute 'window-number-face nil :background "#2a2a2a")
(set-face-attribute 'window-number-face nil :foreground "red")
@JEG2
JEG2 / inheriting_from_base_types.md
Created July 25, 2013 02:53
My thoughts on inheriting from Ruby's base types.

I've been in multiple discussions lately about the problems with subclassing Ruby's base types.

The main problem is that Ruby sometimes takes shortcuts with the base types, for performance reasons. This can cause them to behave odd. For example, have a look at this subclass:

class MyString < String
  def initialize(*)
    super
    puts "Building MyString:  #{inspect}"
  end
@JEG2
JEG2 / struct.md
Created June 3, 2013 21:50
Thinking out loud about the merits and drawbacks of two different usages of Struct…

How Should We Use Struct?

The Choice

It's common in Ruby to see some code setup a Struct like this:

class Specialized < Struct.new(:whatever)
  # ... define custom methods here...
end