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 / quiz61.html
Created March 19, 2013 17:03
The "Dice Roller" Ruby Quiz
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ruby Quiz - Dice Roller (#61)</title>
<link rel="stylesheet" type="text/css" href="quiz.css" />
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
</head><body>
<div id="page">
<div id="header"><span class="ruby">Ruby</span> <span class="quiz">Quiz</span></div>
@JEG2
JEG2 / prepend.rb
Created March 27, 2013 16:18
Showing prepend in the middle.
module Plugin
def some_method
puts "Plugin"
super
end
end
class A
def some_method
puts "A"
@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
@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 / 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 / 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 / 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 / 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![]}
}