Kickstart your career with best deals on top training courses NY10 Click to Copy

Ruby Interview Questions

Prepare better with the best interview questions and answers, and walk away with top interview tips. These interview questions and answers will boost your core interview skills and help you perform better. Be smarter with every interview.

  • 4.6/5 Rating
  • 22 Question(s)
  • 30 Mins of Read
  • 3274+ Reader(s)

Beginner

The command to get installed Ruby version in your system is $ ruby -v ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux] 

Ruby files are renamed using rename method and deleted using delete method . 

To rename a file, following syntax is used. 

Syntax: 

File.rename("oldFile.txt", "newFile.txt") 

To delete a file, following syntax is used. 

Syntax: 

File.delete("file.txt") 

================================================================== 

The four types of variables in Ruby are as follows: 

Global variables begin with $ and are accessible from anywhere within the Ruby program regardless of where they are declared—it stands to reason that they must be handled with care. 

Local variables begin with a lowercase letter or an underscore. The scope of a local variable is confined to the code construct within which it is declared. 

Class variables begin with @@ and are shared by all instances of the class that it is defined in. 

Instance variables begin with @ and are similar to class variables except that they are local to a single instance of a class in which they are instantiated. 

In Ruby, methods may either be public, protected, or private. 

Public methods enforce no access control -- they can be called in any scope. 

Protected methods are only accessible within their defining class and its subclasses. 

Private methods can only be accessed and viewed within their defining class . They are only accessible within the context of the current object

The different class libraries used in Ruby are: 

  • Text processing 
  • CGI Programming 
  • Network programming 
  • GUI programming 
  • XML programming 

Advanced

Both the methods are used to open a file in Ruby 

Difference between both the methods is that File.new method cannot be associated with a block whereas File.open method can be 

File.new method: Using this method a new file can be created for reading, writing or both. 

Syntax: 

f = File.new("fileName.rb") 

File.open method : Using this method a new file object is created. That file object is assigned to a file. 

Syntax: 

#!/usr/bin/ruby 

File.open('about', 'w') do |f| 

f.puts "This is KnowledgeHut" 

f.write "You are reading Ruby interview questions\n" 

f << "Please visit our website.\n" 

end 

freeze method (Object.freeze) is used to prevent an object from being changed. 

water.freeze 

if( water.frozen? ) 

puts "Water object is a frozen object" 

else 

puts "Water object is a normal object" 

end 

In Ruby, the lifecycle of a single thread starts automatically as soon as CPU resources are available. The thread runs the code in the block where it was instantiated and obtains the value of the last expression in that block and returns it upon completion. Threads use up resources but running multiple threads at a time can improve an app’s performance. 

Thread pooling is a technique wherein multiple pre-instantiated reusable threads are left on standby, ready to perform work when needed. 

Thread pooling is best used when there are a large number of short tasks that must be performed. This avoids the overhead of having to create a new thread every time a small task is about to be performed. 

Built-in subclasses of exception are as follows: 

  • No MemoryError 
  • ScriptError 
  • SecurityError 
  • SignalException 
  • StandardError 
  • SystenExit 
  • SystemStackError 
  • fatal - impossible to rescue 

The different iterators used in Ruby are: 

  • each iterator 
  • times iterator 
  • upto and down to iterator 
  • step iterator 
  • each_line iterator 

# Add the gem in Gemfile 

gem 'rack', :github => 'rack/rack', :branch => 'master' 

# Execute the below command in your terminal 

$ bundle config local.rack ~/Work/git/rack 

Ruby strings can be compared with three operators: 

  • == operator Returns true or false 
  • eql? operator Returns true or false 
  • casecmp method Returns 0 if matched or 1 if not matched 

Ruby object 

Object is the default root of all Ruby objects. Ruby objects inherit from BasicObject (it is the parent class of all classes in Ruby) which allows creating alternate object hierarchie 

Objects in Ruby are created by calling new method of the class. It is a unique type of method and predefined in the Ruby library. 

Ruby objects are instances of the class. 

objectName = className.new 

We have a class named Circle. Syntax to create an object circle : 

circle = Circle.new("10") 

In Ruby, load and require both are used for loading the available code into the current code. 

require` reads and parses files only once, when they were referenced for the first time. 

`load` reads and parses files every time you call `load`. 

require searches for the library in all the defined search paths and also appends .rb or .so to the file name you enter. It also makes sure that a library is only included once. So if your application requires library A and B and library B requries library A too A would be loaded only once. 

With load you need to add the full name of the library and it gets loaded every time you call load - even if it already is in memory 

nil false nil cannot be a value. false can be a value. nil is returned where there is no predicate. 

in case of a predicate, true or false is returned by a method. nil is not a boolean data type. false is a boolean data type. nil is an object of nilclass. false is an object of falseclass. 

hsh.keys.map(&:to_s).sort_by(&:length) 

or: 

hsh.keys.collect(&:to_s).sort_by { |key| key.length } 

or : 

def key_sort hsh 

hsh.keys.collect(&:to_s).sort { |a, b| a.length <=> b.length } end 

There are multiple ways to do this, but one possible answer is: 

(1..20).inject( [0, 1] ) { | fib | fib << fib.last(2).inject(:+) } 

As you go up the sequence fib, you sum, or inject(:+), the last two elements in the array and add the result to the end of fib. 

Note: inject is an alias of reduce 

  • include mixes in specified module methods as instance methods in the target class 
  • extend mixes in specified module methods as class methods in the target class 
  • Array#each method iterates over the elements of the array and executes the provided block each time. However, it returns the original array unaffected. 
  • Array#map will return a new array of elements containing the values returned by the block it is provided. It also does not affect the original array 

Array#compact removes nil values. 

Example: 

>> [nil,789,nil,"test"].compact => [789, "test"] 

A call to super invokes the parent method with the same arguments that were passed to the child method. An error will therefore occur if the arguments passed to the child method don’t match what the parent is expecting. 

A call to super() invokes the parent method without any arguments, as presumably expected.