Rails is the well known and popular web framework built upon ruby. Ruby’s Exceptional Creatures is a resource from HoneyBadger that documents common ruby exception.

Improving Ruby Test Performance particularly RSpec test suites.

Fixing implicit declaration error during ruby gem build1

gem install mailcatcher -- --build-flags --with-cflags="-Wno-error=implicit-function-declaration"

Getting the memory size of an objecthacks

puts RUBY_VERSION #=>1.9.3
 
require 'objspace'
 
p ObjectSpace.memsize_of("a"*23)    #=> 23
p ObjectSpace.memsize_of("a"*24)    #=> 24
p ObjectSpace.memsize_of("a".*1000) #=> 1000
h = {"a"=>1, "b"=>2}
p ObjectSpace.memsize_of(h)         #=> 116
  • Note that this doesn’t do anything for finding the references of an object

Delegation

Memory bloat and management

Halve Your Memory Usage with these 12 Weird Tricks is a conference topic by Nate Berkopec on rails app memory techniques

Setting up a Ruby Gem

As of early 2024:

  1. bundle gem <NEW GEM NAME> will create the new gem folder with rspec setup properly.
  2. Add a CHANGELOG.md file.
  3. Create a new github repo matching the gem name, set it in the gemspec and then use that to set subsequent links
  4. Create a LICENSE file and set the license in the gemspec
  5. Add CI with github actions
  6. Add Coverage with coveralls and CI
  7. Setup gem publishing with github actions

Better command line option parsing in rake taskshacks

When passing args to a rake you end up with positional args being your only option, and you have to do some escaping in zsh, so a rake task call looks like this:

bundle exec rake some_task\[1\]

If you would like to use named args, you can use ARGV and make a call like this:

bundle exec rake some_task -- post_id=1

In your rake task you parse these args:

parsed_args = ARGV.filter_map do |a|
  if /^[A-z_]*=([0-9]*|true|false)/.match?(a)
    k, v = a.split('=')
    v = case v
        when /[0-9]+/
          v.to_i
        when /true/
          true
        when /false/
          false
        else
          v
        end
    [k.to_sym, v]
  end
end.to_h

This will give you an array like:

{
  post_id: 1
}

Warning!

This will do some strange things to gets. The solution here is to call $stdin.gets rather than the naked gets