Wednesday, August 29, 2012

Eliminating if else statement using Blocks in Ruby


def foo(a)
  if a == 1
    yield
  else
    42
  end
end

The above method customizes by using a block like this:

result = foo(1) do
  98
end

p result

Wednesday, August 22, 2012

How to stop continuous running fan in MacBook Pro

My MacBook Pro with 8 GB 1067 MHz DDR3 RAM and 3.06 GHz Intel Core 2 Duo was running the fan continuously for couple of weeks. To fix it, I ran:

$ top

in the terminal. I found two ruby processes had been running for 160 hours continuously and were taking up 93% of the CPU usage. I killed them by doing:

$ kill -9 [process-id]

As soon as those two stray processes were killed, laptop became quiet again.


How to install tmux 1.6 on Mac OS

I was having difficulty installing the version 1.6, the older 1.4 version was being picked up. The fix:

1. /opt/bin should be in the path. You can append this to your path by editing the ~/.profile file. Mine looks like this:

##
# Your previous /Users/bparanj/.profile file was backed up as /Users/bparanj/.profile.macports-saved_2010-06-07_at_22:52:52
##

# MacPorts Installer addition on 2010-06-07_at_22:52:52: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:/opt/bin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

2. curl -OL http://downloads.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.19-stable.tar.gz

3. tar xzf libevent-2.0.19-stable.tar.gz

4. cd libevent-2.0.19-stable

5. ./configure --prefix=/opt

6. make

7. sudo make install

8. tar xzf tmux-1.6

9. cd tmux-1.6

10. LDFLAGS="-L/opt/lib" CPPFLAGS="-I/opt/include" LIBS="-lresolv" ./configure --prefix=/opt

11. make && sudo make install

12 which tmux 

showed that the older 1.4 version was being picked up from /opt/local/bin, I deleted it by : sudo rm /opt/local/bin/tmux


How to check tmux version

$ tmux -V

Saturday, August 11, 2012

Rails 3.2 Errno::EACCES Permission Denied when uploading files

1. Make sure the directory has the right permission:

   chmod -R 777 uploads

2. The uploads directory is at the same level as app directory. You can change the upload location from the public folder to any folder by customizing the store_dir in your uploader class:


 def store_dir
     "#{Rails.root}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

3. You also have to define cache_dir in your uploader class, otherwise it will still throw exception.


  def cache_dir
    "#{Rails.root}/tmp/uploads/cache/#{model.id}"
  end