Skip to content

Instantly share code, notes, and snippets.

View DavidEGrayson's full-sized avatar

David Grayson DavidEGrayson

View GitHub Profile
@DavidEGrayson
DavidEGrayson / test_spec.rb
Created October 26, 2012 18:27
RSpec lets you call should_receive and such on normal objects
class Foo
def query(q)
puts "Foo received query #{q}"
end
alias query_alternate query
end
describe RSpec do
before :all do
@DavidEGrayson
DavidEGrayson / idea1.rb
Created January 13, 2013 22:05
These are my ideas for how to implement helpers using Ruby 2.0 refinements. See http://blog.davidegrayson.com/2013/01/ruby-20-helpers-should-be-refinements.html
# This fails. I think the refinements are loaded into the scope
# only when #using is called, and additional refinements that are
# added to the Helpers module later have no effect.
module Helpers
refine Class do
def helper(mod)
klass = self
Helpers.module_exec do
puts "#{klass} wants to use #{mod}, self=#{self}"
PololuLedStrip<11> ledStrip11;
PololuLedStripBase * ledStrips[] =
{
&ledStrip11,
&PololuLedStrip<12>(), // warning: taking address of temporary [-fpermissive]
};
@DavidEGrayson
DavidEGrayson / error.txt
Created January 16, 2013 00:07
Error I got when trying to load the RedCloth gem 4.2.3 in Ruby 1.9.3 (in Rails 3.2.11)
$ rake
rake aborted!
(<unknown>): did not find expected node content while parsing a block node at line 183 column 9
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:6:in `<module:LATEX>'
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:3:in `<top (required)>'
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `require'
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `<top (required)>'
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `require'
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `<top (required)>'
/home/david/.rvm/gems/ruby-1.9.3-p362@system2/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
@DavidEGrayson
DavidEGrayson / gist:4774407
Last active December 12, 2015 12:49
Is this a bug in Ruby? The BigDecimal#eql? doesn't care about the type of the objects being compared, but generally #eql? for numeric types does care about the types. This is consistent with the documentation but seems wrong, and results in asymmetric behavior as shown below. The docs are here: http://www.ruby-doc.org/core-1.9.3/Numeric.html#met…
1.9.3p374 :002 > BigDecimal('6').eql? 6
=> true
1.9.3p374 :003 > 6.eql? BigDecimal('6')
=> false
@DavidEGrayson
DavidEGrayson / JRuby irb
Created April 1, 2013 21:08
There are two ways to load a Java class from JRuby. The one that uses camel-case constants gives a much more helpful error message when something goes wrong loading the class. See below.
irb(main):024:0> com.microchip.mplab.mdbcore.platformtool.PlatformToolMetaManager
NameError: missing class or uppercase package name (`com.microchip.mplab.mdbcore.platformtool.Platfo
rmToolMetaManager')
from org/jruby/javasupport/JavaUtilities.java:54:in `get_proxy_or_package_under_package'
from file:/C:/jruby-1.7.3/lib/jruby.jar!/jruby/java/java_package_module_template.rb:14:in `m
ethod_missing'
from (irb):24:in `evaluate'
from org/jruby/RubyKernel.java:1066:in `eval'
from org/jruby/RubyKernel.java:1409:in `loop'
from org/jruby/RubyKernel.java:1174:in `catch'
@DavidEGrayson
DavidEGrayson / spec.rb
Last active December 15, 2015 19:49
Why you might prefer to use == instead of eq in rspec.
describe "rspec syntax" do
describe "==" do
it "allows line breaks" do
5.should ==
5
end
end
describe "eq" do
it "does not allow line breaks" do
@DavidEGrayson
DavidEGrayson / spec.rb
Created April 26, 2013 18:20
Why you might prefer to use eq instead of == in rspec.
describe 'rspec syntax' do
let(:x) { 4 }
describe '==' do
it 'silently fails if you forget .should' do
x == 5
end
end
describe 'eq' do
module YARD
module Parser
module Ruby
# Builds and s-expression by creating {AstNode} objects with
# the type provided by the first argument.
#
# @example An implicit list of keywords
# ast = s(s(:kw, "if"), s(:kw, "else"))
# ast.type # => :list
# @example A method call
@DavidEGrayson
DavidEGrayson / output.txt
Created August 4, 2013 21:12
dumb error trying to make a .gem file with JRuby in Windows
C:\Users\David\Documents\yard_david>jruby -S rake gem
rake aborted!
["(eval)"] are not files
C:/Users/David/Documents/yard_david/Rakefile:11:in `(root)'
org/jruby/RubyProc.java:255:in `call'
org/jruby/RubyArray.java:1617:in `each'
org/jruby/RubyArray.java:1617:in `each'
org/jruby/RubyKernel.java:1073:in `load'
Tasks: TOP => gem
(See full trace by running task with --trace)