Friday, 16 August 2013

Gotcha: irb, multi-line strings and "Display all 309 possibilities? (y or n)"

Small thing I figured out. I was in irb and trying to copy/paste some text into a multi-line string to play with gsub, and it kept exploding halfway through and showing the message:

Display all 309 possibilities? (y or n)

instead of actually allowing me to create a multi-line string.

Turns out, irb still responds to tab-completion even if you're halfway through creating a multi-line string.

This kinda sucks if you want a string that contains tabs - but don't want to manually type the whole string (including tabs) yourself as you go along.

To fix it, I copy/pasted the text into vim and used :retab to replace the tabs with spaces before copying it back into the string in irb... its a hack, but it worked for my purposes.

Any thoughts on a better solution?

Wednesday, 7 August 2013

Link: alias vs alias_method

If you ever wondered what the difference was, this quick post by Neeraj Singh will show you the difference between alias and alias_method

TL;DR:

  • alias_method is slightly more flexible accepting strings or symbols
  • alias_method will redefine local methods at runtime - which means you can override the aliased method in a subclass and alias_method will pick this up
  • alias will keep the original scope when aliasing - so even if you override the method in a subclass, the aliased method will refer to the parent's original method
  • alias works nicer with rdoc

Don't forget to use a comma for alias_method