Skip to content

Instantly share code, notes, and snippets.

View niklas's full-sized avatar

Niklas H niklas

View GitHub Profile
@niklas
niklas / class-eval-yield.rb
Created May 25, 2011 15:16
yielding to a block within class_eval does not set self in the block?
class A
def self.my_eval
B.class_eval do
puts "self in A.my_eval: #{self}"
yield
end
end
end
class B
@niklas
niklas / trace.txt
Created April 26, 2011 14:52
undefined local variable or method `o' for Gherkin::Rubify:Module
undefined local variable or method `o' for Gherkin::Rubify:Module
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/gherkin-2.3.6/lib/gherkin/rubify.rb:19
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `load_dependency'
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in `load_dependency'
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
/var/lib/hudson/.rvm/gems/ree-1.8.7-2010.02/gems/cucumber-0.10.2/lib/cucumber/ast/table.rb:1
/var/lib/hudson/.rvm/gems/
activemodel (3.0.3) lib/active_model/attribute_methods.rb:364:in `method_missing'
activerecord (3.0.3) lib/active_record/attribute_methods.rb:46:in `method_missing'
devise (1.1.2) lib/devise/models/database_authenticatable.rb:40:in `password='
activerecord (3.0.3) lib/active_record/base.rb:1559:in `send'
activerecord (3.0.3) lib/active_record/base.rb:1559:in `attributes='
activerecord (3.0.3) lib/active_record/base.rb:1555:in `each'
activerecord (3.0.3) lib/active_record/base.rb:1555:in `attributes='
activerecord (3.0.3) lib/active_record/base.rb:1406:in `initialize'
adva-core (0.0.9) lib/patches/rails/sti_associations.rb:13:in `new'
adva-core (0.0.9) lib/patches/rails/sti_associations.rb:13:in `build_association'
@niklas
niklas / lxc.rb
Created April 24, 2011 16:44
Setup an lxc-container (chef recipe)
package 'debootstrap'
package 'apt-cacher-ng'
package 'inotify-tools'
include_recipe 'lxc::manage'
include_recipe 'lxc::network_bridge'
include_recipe 'lxc::dns'
host = node[:container]
@niklas
niklas / simple_form_form_builder.rb
Created April 20, 2011 14:42
get real buttons in forms that is stylable
# now you can use
# form.button :submit
# to get a real button that is stylable
class SimpleForm::FormBuilder
def submit_button(title=nil, options={}, &block)
title, options = nil, title if title.is_a?(Hash)
title ||= submit_default_value
options[:type] = 'submit'
template.add_class_to_html_options(options, 'submit')
@niklas
niklas / ruby_rvm_system_invocation.rb
Created April 8, 2011 12:31
How to run a system command from ruby within a clean bash that has never sourced rvm using a custom ruby version
#!/usr/bin/env ruby
# How to run a system command from ruby within a clean bash that has never sourced rvm using a custom ruby version
# 1) install rvm as the correct user
# 2) do NOT follow the instructions about sourcing and adding this to your .bashrc
# 3) run the following (change the versions accordingly)
ruby_versions = %w(ruby-1.8.7-p330 rbx-head)
@niklas
niklas / rails-fields-with-errors-get-class-by-nokogiri.rb
Created January 9, 2011 21:03
Add .error CSS class to all invalid form fields. Rails wraps them in div.fieldWithErrors, which still sucks in 3.0
# put this in config/initializer/fields_with_errors.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
error_class = 'error'
doc = Nokogiri::XML(html_tag)
doc.children.each do |field|
unless field['type'] == 'hidden'
unless field['class'] =~ /\berror\b/
@niklas
niklas / sizeof64.c
Created January 3, 2011 22:50
trying to find out if ruby 1.9.2 does not implement syscall() on amd64 because of the different sizes if int and long
/*
please do me a favour: do the following on a amd64 ubuntu
$ gcc -o sizeof64 sizeof64.c
$ ./sizeof64
are they different? would this be OK? If not, what could cause this?
If it is OK, why is ruby 1.9 not able to implement syscall()
https://github.com/ruby/ruby/blob/ruby_1_9_2/io.c#L7529
# works with textnodes, too
Nokogiri::XML::Node.class_eval do
def to_hash
unless children.empty?
h = children.to_hash
if h.length == 1 && h.has_key?(:onlytext)
{ name => h[:onlytext] }
else
{ name => children.to_hash }