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:

ActiveRecord::PendingMigrationError

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.

Advertisements

Debugging Methods

Here are few methods that we can use for debugging:

1) Kernal#Caller

 caller.inspect

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)
m.class
=> Method
m.call
=> "HELLO WORLD"

3) source_location

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

Dog.new.method(:bark).source_location

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

4) ancestors

"foo".class.ancestors
=> [String, Comparable, Object, Kernal, BasicObject]

In class method you can simply call it like:

self.class.ancestors

5) Module#method_defined?

"foo".class.method_defined?(:upcase)
=> true
99.class.method_defined?(:upcase)
=> false

6) Method#instance_method

Instead of

User.new.method(:github_url).source_location

you can use:

User.instance_method(:github_url).source_location

.

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
reconstituted.
Rails uses marshaling to store session data.

Experiement Test Case

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

  beforeEach(function() {
    browser.get('app/index.html#/phones/nexus-s');
  });
  it('should display atleast 4 images', function(){
    var phoneImg = element.all(by.repeater('img in phone.images'));
    expect(phoneImg.count()).toBeGreaterThan(3);
  });
});

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 https://raw.github.com/creationix/nvm/v0.4.0/install.sh | 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

Ref:
https://github.com/creationix/nvm
http://docs.angularjs.org/tutorial

TheRequest Interview Question’s Answer

1)
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.

Solution:

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

example([:dog,:cat,:orange,:banana])

Output:

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

2)
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.

Solution:

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

puts find_chars("test", "string")

Output:

ts