Setup Existing Project

  • Install rbenv if not installed.
  • create two hidden files .ruby-version and .rbenv-gemsets in main/root folder of project.
    • add ruby version in .ruby-version file. e.g: 2.2.1
    • give a name to gemset where all the gems will be install for this project in .rbenv-gemsets. e.g: abc
  • Open terminal and go to project directory using ‘cd’ command.
    • run command: ‘rbenv gemset active’
    • it will display the gemset name which you just mentioned in .rbenv-gemset file.
    • install bundler using “gem install bundler” if not installed
    • run command: ‘bundle install’
    • install missing libs if bundle install does not get succeeded.
  • Open database.yml and edit it according to your local setup.
  • Again switch back to terminal:
    • create database using: ‘rake db:create’
    • run migration: ‘rake db:migrate’
    • run seed: ‘rake db:seed’

Different ways of Iterating over an Array in Ruby (Just for me)

1) .each do |val| … end

2) .each_with_index do |val, index| … end

3) a) .map do … end  => returns new array

b) .map.with_index do |val, index| … end

4) .any? do |val| {condition} end   => returns true or false

5) .all? do |val| {condition} end   => returns true if all value satisfy the condition otherwise false

6) .select do |val| {condition} end   => returns an array of those values which satisfy condition.

7) .find do |val| {condition} end   => returns first value which passes condition.

8) .find_all do |val| {condition} end   => returns all those value which satisfy conditon. It’s equivalent to .select

9) .inject([initial_value_of_var1]) do |var1, var2 | … end => returns var1

 

Loops and Repetitions in Ruby

1) times:

[integer].times do |i| ... end

e.g:

5.times do |i|
  puts "iteration: #{i}"
end

2) upto:

[start_val].upto [end_val] do |i| ... end

e.g:

1.upto 5 do |i|
  puts "iteration: #{i}"
end

3) downto:
it’s inverse of upto.

[end_val].downto [start_val] do |i| ... end

e.g:

5.downto 1 do |i|
  puts "iteration: #{i}"
end

4) step:

We can do both upto and downto with step loop. It’s very flexible.

a) [start_val].step [end_val] do |i| ... end
e.g:
1.step 5 do |i|
  puts "Iteration: #{i}"
end

for stepping:

b) [start_val].step [end_val], [incremental_val] do |i| ... end
e.g: 
1.step 5, 2 do |i|
  puts "Iteration: #{i}"
end

like downto:

c) [end_val].step [start_val], -1 do |i| ... end
e.g: 
5.step 1, -1 do |i|
  puts "Iteration: #{i}"
end

5) while
a)

while conditional [do]
  code
end

e.g:
i = 1
while i <= 5 do
  puts "Iteration: #{i}"
  i += 1
end

b)

code while condition

OR

begin 
  code 
end while conditional

e.g:
i = 1
begin
  puts "Iteration: #{i}"
  i +=1
end while i < 5

7) until
It’s similar to while but having reverse condition.

e.g:
i = 1
until i > 5 do
  puts "Iteration: #{i}"
  i += 1
end

8) for

for [var_name] in [rang] do ... end
e.g: 
for i in (1..10) do
  puts "Iteration: #{i}"
end

9) each

[rang].each do |[var]| ... end
e.g: 
(1..10).each do |i|
  puts "Iteration: #{i}"
end

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.

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.

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