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.
$ 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
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
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
Monday, July 16, 2012
Error: This transaction cannot be processed due to an invalid merchant configuration.
ActiveMerchant 1.26, Rails 3.2
Resolution:
1. Create your selling test account using the Preconfigured option.
2. Under Account Type, select Website Payments Pro.
3. My Account --> Profile --> API access to get API username, API Password and Signature
Resolution:
1. Create your selling test account using the Preconfigured option.
2. Under Account Type, select Website Payments Pro.
3. My Account --> Profile --> API access to get API username, API Password and Signature
Remember the API credentials are different from the test login account. You have to save the login password to the Sandbox account in Step 1 above. There is no way to go back and view the login password to the Sandbox account.
Sunday, July 08, 2012
remove github prompt when checkin
Change the url that has https to :
url = git@github.com:your_github_username/your_project.git
url = git@github.com:your_github_username/your_project.git
Saturday, June 23, 2012
TDD Screencasts by Kent Beck
1. Test for remove operation does not fail. He writes a test and does not run it, he goes to the implementation and implements. Then he runs the test and it passes.
2. Interesting to see how he uses the debugger to learn about the API and uses that information to write a test.
2. Interesting to see how he uses the debugger to learn about the API and uses that information to write a test.
Friday, June 22, 2012
Skip Bundle Install and Test Unit When Creating Rails 3.2 project
rails new ipn --skip-bundle --skip-test-unit
Wednesday, June 06, 2012
github push is prompting for username and password
I was getting this error for https://github.com/bparanj/mongodb_specs repo. Fix :
$git config remote.origin.url git@github.com:bparanj/mongodb_specs.git
$git config remote.origin.url git@github.com:bparanj/mongodb_specs.git
Thursday, May 31, 2012
-bash: mongod: command not found
export PATH=$PATH:/usr/local/mongodb/bin
For Mongodb 2.0.3
mongod --dbpath /Users/bparanj/data/mongodb
The dbpath is the path to the mongodb.config file
which contains :
dbpath=/Users/bparanj/data/mongodb
Deploying to a VPS
To get the http://railscasts.com/episodes/335-deploying-to-a-vps?view=asciicast working I had to do :
1. apt-get install sqlite3 libsqlite3-dev
2. Add : gem 'pg' and gem 'unicorn' to Gemfile
1. apt-get install sqlite3 libsqlite3-dev
2. Add : gem 'pg' and gem 'unicorn' to Gemfile
Wednesday, May 30, 2012
undefined local variable or method `autotest' for main:Object
This error happens when you have this : require autotest/fsevent
in your ~/.autotest file, change it to : require 'autotest/fsevent'
Thursday, May 10, 2012
Data Driven Unit Test
How do you avoid loops in your specs?
Like Alex points out, this is a test smell according to Gerard Mezos. In .NET they have something called as data driven unit test. In Ruby we can accomplish the same thing using the following:def data_driven_spec(container)
container.each do |element|
yield(element)
end
end
and in your test you can do :
it "should return back slash pre-pended to all special characters" do
SPECIAL_CHARACTERS = ["?", "^", "$", "/", "\\", "[", "]", "{", "}", "(", ")", "+", "*", "." ]
data_driven_spec(SPECIAL_CHARACTERS) do |special_character|
result = Regexpgen::Component.match_this_character(special_character)
result.should == "\\#{special_character}"
end
end
The code is taken from my regexpgen ruby gem. My test differs from the message that goes in to the it method. I have raised the level of abstraction, whereas Alex substitues the a variable to vary the message.
Wednesday, May 09, 2012
Creating a Ruby gem with RSpec as the test framework
1. bundle gem 'your-gem-name'
2. Run : rspec --init from your gem directory to generate the spec/spec_helper.rb and .rspec files.
.rspec has the configuration such as color and format of the output when specs are run.
2. Run : rspec --init from your gem directory to generate the spec/spec_helper.rb and .rspec files.
.rspec has the configuration such as color and format of the output when specs are run.
Tuesday, May 01, 2012
irb tricks
1. touch ~/.irbrc
2. Edit the .irbrc and add the following code:
require 'pp'
require 'rubygems'
class Object
def ls
%x{ls}.split("\n")
end
def pwd
Dir.pwd
end
def cd(dir)
Dir.chdir(dir)
pwd
end
end
alias p pp
3. Now whenever you open an irb session you can now list the directory contents by doing ls, print working directory by pwd and change directory by doing cd "directory-name".
2. Edit the .irbrc and add the following code:
require 'pp'
require 'rubygems'
class Object
def ls
%x{ls}.split("\n")
end
def pwd
Dir.pwd
end
def cd(dir)
Dir.chdir(dir)
pwd
end
end
alias p pp
3. Now whenever you open an irb session you can now list the directory contents by doing ls, print working directory by pwd and change directory by doing cd "directory-name".
Friday, April 27, 2012
Solution for Ruby Koans
You can download by solution from here https://github.com/bparanj/ruby-koans-edgecase.
This took me 5.5 hours to complete. I broke this down into 11, 30 minutes sessions over a period of two weeks.
Things that I learned:
1. It was a "aha" moment when I worked through a series of tests that described the behavior of a class. You have to do the exercises to get the feel for describing the behavior or specification of a class.
2. It helps you to find your strengths and weakness so you will know where to focus your learning efforts.
This took me 5.5 hours to complete. I broke this down into 11, 30 minutes sessions over a period of two weeks.
Things that I learned:
1. It was a "aha" moment when I worked through a series of tests that described the behavior of a class. You have to do the exercises to get the feel for describing the behavior or specification of a class.
2. It helps you to find your strengths and weakness so you will know where to focus your learning efforts.
How to install rubygems from source on Ubuntu 10.04
1. wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz
2. Extract : tar xvzf rubygems-1.3.6.tgz
3. cd to rubygems directory and run : ruby setup.rb
4. sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
Check version by either : gem -v or gem env
2. Extract : tar xvzf rubygems-1.3.6.tgz
3. cd to rubygems directory and run : ruby setup.rb
4. sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
Check version by either : gem -v or gem env
`merge': can't convert String into Hash
This error happens when you have syntax error in your .gemrc file. Fix it. gem env will work again.
Subscribe to:
Posts (Atom)