About Me

My photo
जिंदगी की परीक्षा में कोई नम्बर नहीं मिलते है लोग आपको दिल से याद करे तो समझ लेना आप पास हो गए....

Friday 16 October 2015

Ruby interview questions

Ruby overview :
An open source programming language that focuses on simplicity and productivity. The syntax of Ruby language is elegant which is natural to read and easy to write. Ruby is well known as language of careful balance. It has the blended features of Perl, Small talk, Eiffel, Ada and Lisp.

That ruby program use less code and are more flexible.


Interpolation:
 Interpolation is the process of inserting a string into a literal.

Require in ruby?
Require() loads and processes the Ruby code from a separate file, including whatever classes, modules, methods, and constants are in that file into the current scope.

Difference between nil and false?
False is a boolean data type
Nil is not a data type

difference between classes and modules?

1.Modules serve as a mechanism for name spaces.
module ANamespace
  class AClass
    def initialize
      puts "Another object, coming right up!"
    end
  end
end

ANamespace::AClass.new
 #=> Another object, coming right up!
Also, modules provide as a mechanism for multiple inheritance via mix-ins and cannot be instantiated like classes can.
module AMixIn
  def who_am_i?
    puts "An existentialist, that's who."
  end
end

# String is already the parent class
class DeepString < String
  # extend adds instance methods from AMixIn as class methods
  extend AMixIn
end

DeepString.who_am_i?
 #=> An existentialist, that's who.

AMixIn.new
 #=> NoMethodError: undefined method ‘new’ for AMixIn:Module

There are three ways to invoke a method in ruby. Can you give me at least two?

the dot operator (or period operator), the Object#send method, or method(:foo).call
object = Object.new
puts object.object_id
 #=> 282660

puts object.send(:object_id)
 #=> 282660

puts object.method(:object_id).call # (Kudos to Ezra)
 #=> 282660

 

What does self mean?

self always refers to the current object. But this question is more difficult than it seems becauseClasses are also objects in ruby. (Kudos to Stephen)
class WhatIsSelf
  def test
    puts "At the instance level, self is #{self}"
  end

  def self.test
    puts "At the class level, self is #{self}"
  end
end

WhatIsSelf.test 
 #=> At the class level, self is WhatIsSelf

WhatIsSelf.new.test 
 #=> At the instance level, self is #<WhatIsSelf:0x28190>

 

What is a Proc?

Essentially, Procs are anonymous methods (or nameless functions) containing code. They can be placed inside a variable and passed around like any other object or scalar value. They are created byProc.new, lambda, and blocks (invoked by the yield keyword).

# wants a proc, a lambda, AND a block
def three_ways(proc, lambda, &block)
  proc.call
  lambda.call
  yield # like block.call
  puts "#{proc.inspect} #{lambda.inspect} #{block.inspect}"
end

anonymous = Proc.new { puts "I'm a Proc for sure." }
nameless  = lambda { puts "But what about me?" }

three_ways(anonymous, nameless) do
  puts "I'm a block, but could it be???"
end
 #=> I'm a Proc for sure.
 #=> But what about me?
 #=> I'm a block, but could it be???
 #=> #<Proc:0x00089d64> #<Proc:0x00089c74> #<Proc:0x00089b34>

  Tools to profile ruby code?

like the unix time command, the ruby Benchmark class, and the ruby library called ruby-prof.

 

What are rubygems?

rubygems is package manager software for ruby libraries 

 






References: 
http://www.careerride.com/Ruby-Interview-Questions.aspx 
https://gist.github.com/ryansobol/5252653