pahanix & IT

yet another passionate programmer

3 notes &

Clear your logs automatically in development when they are too large.

# config/initializers/clear_logs.rb

# This snippet simply clears your logs when they are too large.
# Large logs mean looooong search in TextMate. You know it :)
# Every time you run rails server or rails console it checks log sizes 
# and clears the logs for you if necessary.

if Rails.env.development?
  MAX_LOG_SIZE = 2.megabytes
  logs = File.join(Rails.root, 'log', '*.log')
  if Dir[logs].any? {|log| File.size?(log).to_i > MAX_LOG_SIZE }
    $stdout.puts "Runing rake log:clear"
    `rake log:clear`
UPD: It will delete production logs if you mistakenly run something in dev mode on production

Filed under rails ruby tips

16 notes &

Interesting commits #1

Devise. [dd721f] Use secure compare as well.

# constant-time comparison algorithm to prevent timing attacks
def self.secure_compare(a, b)
  return false unless a.present? && b.present?
  return false unless a.bytesize == b.bytesize
  l = a.unpack "C#{a.bytesize}"

  res = 0
  b.each_byte { |byte| res |= byte ^ l.shift }
  res == 0

In short, a timing attack uses statistical analysis of how long it takes your application to do something in order to learn something about the data it’s operating on. For HMACs, this means using the amount of time your application takes to compare a given value with a calculated value to learn information about the calculated value.

A Lesson In Timing Attacks

Filed under devise ruby

0 notes &

git was compiled without libcurl support.

If you get error “git was compiled without libcurl support" when deploying it probably means that you’ve added a git submodule using http protocol.

git submodule add[repo].git

Open your .gitmodules file and replace[repo].git with git://[repo].git

Next time you add a submodule use command like this:

git submodule add git://[repo].git

Filed under git libcurl git submodule

27 notes &

APIDock from command line


Ruby on Rails developers spend a lot of time in console.

function api() { google-chrome "$1/search?query=$2" ;}

added to your .bashrc file will allow you to search Ruby/Ruby on Rails/Rspec documentation on APIDock directly from the console.


api [ruby|rails|rspec] method_name


api rails link_to

A better way is to search by default in rails and specify ruby|rspec only if needed.

function apidock() {
  if [ $1 = 'ruby' ] || [ $1 = 'rspec' ]; then
    open "$1/search?query=$2" ;
    open "$1";

Filed under ruby bash apidock

4 notes &

How to disable Adobe Reader Safari plugin

On Mac OSX, the default thing that happens when you click a PDF link in Safari is that it is displayed, quickly and efficiently, with the “Preview” tool.

Unfortunately, if you download and install Adobe’s own “Reader” software, it changes Safari’s behaviour to use the crap slow-loading slow-displaying Adobe tool instead. Granted, it can do some other things like PDF forms, but for most PDFs it just gets in the way.

I couldn’t find out from the web how to remove this behaviour without uninstalling the Reader (which you need to have around sometimes), but I worked it out, so here’s how - just delete this file from your hard drive: /Library/Internet Plug-Ins/AdobePDFViewer.plugin

Filed under safari pdf adobe

3 notes &

How a little comma in ruby code can be a pain in the neck

Let assume that you transform a hash

  :a => 1,
  :b => 2

into local variables somewhere in your code and forget to remove a little comma after one line

a = 1,
b = 2

You still have a valid ruby code but it produces something unexpected!

a # => [1,2]
b # => 2

Why? Because of the comma ruby uses multiple assignment operator to assign values. Let write the code into one line to clarify what happens here.

a = 1, b = 2
a = 1, (b = 2)
a = 1, 2
a # => [1,2]

Next time you see an unexpected array, search for a comma!

Filed under ruby comma multiple assignment parallel assignment

1 note &

Let Hoptoad know about your failed rake tasks

If you use Hoptoad and run rake tasks from cron you won’t receive any error messages until you call Hoptoad notifier explicitly.

The correct way to do that is to call: HoptoadNotifier.notify(e)

task :taskname do
    # ...
  rescue Exception => e
    raise # to show error in console

Filed under hoptoad rake task

1 note &

Hoptoad and rescue_from

I’m using rescue_from in my controller, but now I don’t see the exceptions in Hoptoad. Whazzup with that?

Hoptoad uses alias_method_chain to hook into the rescue_action_in_public method.

# Overrides the rescue_action method in ActionController::Base, 
# but does not inhibit any custom processing that is defined 
# with Rails 2's exception helpers.
def rescue_action_in_public_with_hoptoad exception
  notify_hoptoad(exception) unless ignore?(exception)

The rescue_from system in Rails actually catches the exceptions before rescue_action_in_public happens. The upshot is that if you want to see those exceptions in Hoptoad, you’ll need to explicitly send them along in your rescue_from method using notify_hoptoad(exception):

rescue_from SillyError, :with => :render_silly_error

def render_silly_error(e)
  render :template => 'i_blowzed_up'

Filed under rails hoptoad rescue_from

0 notes &

IRHG - Integrated Ruby Hacker's Guide

The RHG is a book that explains how the ruby interpreter (the official C implementation of the Ruby language) works internally.

It is partially translated from Japanese. Official RHG contains only 4 chapters.

The IRHG Project has been significantly improved by the integration of the Ruby Forge Translation Documents. The two projects have been merged and have provided a improved format, especially with the addition of color graphics.

All front-end pages and chapters 1 through 10, and chapter 18 have been completed.

Chapter11 is partially completed.

Part of chapter 11, chapters 12 through 17, and chapters 19 and 20 are presently in machine translated form. Due to the use of Google Translation API’s,  these are much more readable than previously.

Filed under RHG IRGH ruby hacker's guide ruby