Run Server with Pending Migrations

Sometime it happens that you want to run server with pending migrations.

For example you run rake db:rollback

now will have one pending migration and you want to see the effect of that migration and for this you need to run the server. Now if you will try to run server it will give you the error:


Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

so, to get rid of this error you can comment a following line from specific environment file:

# config.active_record.migration_error = :page_load

It raise an error on page load if there are pending migrations.


Debugging Methods

Here are few methods that we can use for debugging:

1) Kernal#Caller


shows you where you have been.

It will give you “file:line in method”

2) Method

It is a class.

m = "Hello World".method(:upcase)
=> Method

3) source_location

It will give address and line number. It shows you where you are going.

It will give you address with line number of this above(:bark) method.

4) ancestors

=> [String, Comparable, Object, Kernal, BasicObject]

In class method you can simply call it like:


5) Module#method_defined?

=> true
=> false

6) Method#instance_method

Instead of

you can use:



here we don’t need to create instance.

Marshaling Objects

Ruby can take an object and convert it into a stream of bytes that can be
stored outside the application. This process is called marshaling.
This saved object can later be read by another instance of the application (or by a totally
separate application), and a copy of the originally saved object can be
Rails uses marshaling to store session data.

Experiement Test Case

describe('Phone detail view', function() {

  beforeEach(function() {
  it('should display atleast 4 images', function(){
    var phoneImg = element.all(by.repeater('img in phone.images'));

Server setup

I am using ubuntu 12.04 so these command works for me to run server:

Install Node Version Manager to manage different versions of node:

curl | sh

Install the latest v0.10.x release of node:

nvm install 0.10

And then in any new shell just use the installed version:

nvm use 0.10

Now go to your project directory and run these following commands. And one more thing that now onward whenever you will need to run server use these commands only, no need to run all above commands.

nvm use 0.10
npm install
npm start

Thats it. Now open browser and hit http://localhost:8000/app/index.html


TheRequest Interview Question’s Answer

Array manipulation in Ruby

Suppose you have the following array

stuff = [:dog,:cat,:orange,:banana]
How can you slice this array to create a new array [:cat,:orange]
Add the element :apple on to the end of the array.
Now take :apple back off again
Add the element :fish to the start of the array.
Now remove the element :fish.


def example(arr=[])
  arr = arr[1..(-2)]
  puts "1. struff=> #{arr.inspect}"
  arr.insert((-1), :apple)
  puts "2. struff=> #{arr.inspect}"
  puts "3. struff=> #{arr.inspect}"
  arr.insert(0, :fish)
  puts "4. struff=> #{arr.inspect}"
  puts "5. struff=> #{arr.inspect}"



1. struff=> [:cat, :orange]
2. struff=> [:cat, :orange, :apple]
3. struff=> [:cat, :orange]
4. struff=> [:fish, :cat, :orange]
5. struff=> [:cat, :orange]

Characters in Strings in Ruby

Implement a function with signature find_chars(string1, string2) that takes two strings and returns a string that contains only the characters found in string1 and string two in the order that they are found in string1. Implement a version of order N*N and one of order N.


def find_chars(string1, string2)
  (string1.split('') & string2.split('')).join

puts find_chars("test", "string")