Using Enumerable::inject to modify a hash

rubyquicktips:

Ever wanted to modify the keys and values of a Hash? Enumerable::inject has you covered.

Try this snippet from Stack Overflow:

my_hash = { a: 'foo', b: 'bar' }
# => {:a=>"foo", :b=>"bar"}
a_new_hash = my_hash.inject({}) { |h, (k, v)| h[k.upcase] = v.upcase; h }
# => {:A=>"FOO", :B=>"BAR"}
(Reblogged from rubyquicktips)

New Relic

Install New Relic into your application and get a free RC Helicopter.

View source of a method in IRB

There is an awesome gem called method_source (used internally in pry).
Example:

Set.instance_method(:merge).source.display
# =>
def merge(enum)
  if enum.instance_of?(self.class)
    @hash.update(enum.instance_variable_get(:@hash))
  else
    do_with_enum(enum) { |o| add(o) }
  end

  self
end

You can also see the comment for a given method!

Set.instance_method(:merge).comment.display
# =>
# Merges the elements of the given enumerable object to the set and
# returns self.

In case of a class (singleton) method, replace

instance_method

with just

method

Google Chrome extension for iluvruby released!

Google Chrome extension for iluvruby blog is out!!! Latest iluvruby posts now just one click away:

Check it out on Github

iluvruby Google Chrome extension screenshot

Excellent introduction to AMQP

Michael Klishin - AMQP model explained

Convert object to array

Have you ever been in a situation where you needed a method that does the following:

  • Converts nil to an empty array
  • Converts non-array variable n to [n]
  • Leaves array variable as is

The way to achieve this is using little known method Array()

> Array(nil)
 => []
> Array([])
 => []
> Array(1)
 => [1]
> Array([2])
 => [2]

Disabling transactions in single rspec test (describe block)

In rspec tests in which you have more than one database connection, uncommitted changes made using one connection are visible only through that connection. Rspec starts a transaction before each test and rolls it back at the end so the database is in the clean state for the next test. This can be turned off, in which case you would have to make sure you clean up the database manually if needed. This change can be applied in the scope of a describe block like this: 

describe Klass do
  self.use_transactional_fixtures = false
  ...
end

Add me on Google+

Google+ so far impressed me. Feel free to add me or post a comment with your email if you need an invitation.

Ruby Symbols Stay in Memory

In ruby, symbols never get garbage collected. They stay in memory until the process exits. Get all the symbols in memory with the following command:

Symbol.all_symbols

Ruby objects to go: Copyrb - Gem for copying and pasting ruby objects

I have written a quick gem that allows you to copy and paste ruby objects across terminals. This gem has been created primarily to simplify the process of copying objects between different rails environments for people that spend a lot of time in the Rails console (like me). Some examples include long strings, or perhaps strings that are hard to copy due to the escaping involved. You might find it handy to copy the ActiveRecord object that has been misbehaving in production, or to copy an array of objects (to seed the test/development database).

  1. Installation is straightforward: gem install copyrb
  2. You will need to require the gem in both (or more) terminals with: require ‘copyrb’
  3. first terminal: copy my_object
  4. second terminal: paste #=> my_object

Feel free to check out the source on Github. Mac OSX only (for now).