Top 45 Ruby interview questions for 2021

Below are the set of top Ruby interview questions for 2021.

Table of Contents

1.What is Ruby?

Ruby is an open-source, object-oriented programming language that is similar to Perl, Smalltalk, Ada, Lisp programming language. It is a simple language that focuses on more productivity.

2. Why Ruby is a flexible programming language?

Ruby allows flexibility for the programmers to modify the programming elements. It also does not restrict the user. This means we can use multiple symbols for the same functionality by updating the built-in class. For example, we can either use + symbol or the word “plus” for adding two numbers. We need to provide this modification in the Numeric built-in class.

3. What are the different features of Ruby?

Ruby programming language has the below different features:

  • Object-oriented
  • Flexible
  • Portable
  • Case sensitive
  • Garbage collection
  • Naming convention

4. What is the command to find the installed Ruby version?

We can use the command ruby -v to find the current version of the Ruby programming language that is installed.

5. What are the different operators in Ruby?

Below are the different operators in Ruby:

  • Arithmetic operator
  • Assignment operator
  • Comparison operator
  • Logical operator
  • Bitwise operator
  • Ternary operator
  • Unary operator
  • Range operator

6. What are the different types of comments in Ruby?

Ruby supports 2 types of comments:

  • Single line comment: We can use # to comment a single line
  • Multiline comment: To comment a group of statements, use =begin at the start and =end at the end of the comment.

7. What are the different types of data types in Ruby?

Ruby supports multiple datatypes as below:

  • Strings
  • Arrays
  • Numbers
  • Boolean
  • Hashes
  • Symbol

8. What are the different variables in Ruby?

  • Class variable
  • Local variable
  • Global variable
  • Instance variable

9. What are the differences between nil and false in Ruby?

It cannot contain a valueIt can be a value
It is not a boolean data typeIt is a boolean data type
It is an object of nilclassIt is an object of falseclass
Returns a nil value when there is no predicateReturns true or false when there is a predicate

10. What is the use of load and require in Ruby?

We can use load and require in Ruby to load the available code into the current code. For autoload, we can use require for any frequent loading, we can use the load command.

11. What are the different iterators in Ruby?

  • step iterator
  • each iterator
  • each_line iterator
  • times iterator
  • upto and downto iterator

12. What are the different ways to add an item to an array?

  • insert
  • push
  • unshift

13. What are the different ways to remove an item from an array?

  • shift
  • uniq
  • pop
  • delete

14. What are the different built-in Ruby class exceptions?

  • SecurityError
  • NoMemoryError
  • SignalException
  • ScriptError

15. What are LDAP operations in Ruby?

  • #add
  • #delete
  • #search
  • #modify
  • #bind
  • #rename

16. What are the different class libraries in Ruby?

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

17. How to create a Ruby object?

We can create a Ruby object using the new method.

ObjectName =

18. What is a Ruby class?

Classes are the first class object in Ruby. It is an instance of Ruby class. We can create a class by specifying the keyword class and end it using the keyword end.

class className

19. What are the methods and how to create a method in Ruby?

A method is similar to a function that helps us to increase code reusability. Each method has a specific functionality. We can create a method using the keyword def and end.

def methodName

20. What is an ampersand parameter(&block) in Ruby?

The & symbol is used to pass a reference variable to a method block instead of the local variable. We can use any name after the & symbol to denote a reference variable.

21. What is a module in Ruby?

A module is a group of methods and constants and may be similar to classes. But we cannot create an object or subclass using the modules. The main use of a module in Ruby is it prevents name clashes and acts as a namespace.

module Modulename

22. What are the different ways to concatenate a string?

Concatenation means joining one or more strings together to form a single string. We can concatenate a string in multiple in Ruby as below.

  • + symbol
  • single space between the strings
  • << sign in between the strings
  • concat method

23. What is a freeze method?

Generally, strings are immutable which means we cannot modify them and need to create a new string every time. We can use the freeze method to make strings immutable.

24. What are the different ways to compare a Ruby string?

  • == operator
  • eql? operator
  • casecmp method

25. What are the different ways to create an array in Ruby?

Arrays are an ordered collection of objects that can hold any object like strings, numbers, hash, symbols, etc. We can create an array by using either of the below methods:

  • literal constructor []
  • new class method

Array index always starts with 0.

26. What are the different methods of IO console in Ruby?

There are different methods that the IO console provides to interact with the console.

  • IO::console
  • IO#raw#raw!
  • IO#cooked
  • IO#cooked!
  • IO#getch

27. What are the different ways to open a file in Ruby?

There are 2 different methods to open a file in Ruby:

  • method
  • method

28. How to rename and delete a file in Ruby?

To rename file, we can use the rename method and to delete a file, we can use the delete method.



29. How to handle exceptions in Ruby?

We can handle exceptions by specifying the code within the begin-end block. We can capture the exceptions using the rescue clause.

30. What is the use of a retry statement?

The retry statement re-executes the code from the begin statement after it catches the exception in the rescue clause. Generally, after the exception is captured, it resumes after the begin code.

#capture exceptions
retry #reexecute from the begin block

31. What is the use of the raise statement?

We can use the raise statement to raise an exception.

raise "Error message"


raise ExceptionType, "Error message"


raise ExceptionType, "Error message" condition

32. What is the use of ensure statement?

The ensure statement ensures that the code executes irrespective of whether an exception occurs or not. This means it always executes the ensure code after the rescue block.

#capture exception
#executes always

33. What are white space characters in Ruby?

Whitespace characters interpret ambiguous statements. It may be either space or a tab.

34. What is the extension of the Ruby programming language?

The extension of Ruby programming language is .rb

35. What are the two types of XSLT parsers available in Ruby?

  • XSLT4R
  • Ruby-Sablotron

36. What is the syntax to check if a directory exists or not in Ruby?

puts dir.exists? "dirname"

37. What are the different access level controls for Ruby methods?

  • public: accessible everywhere
  • protected: accessible within the class and its subclasses.
  • private: accessible only within the defining class.

38. What are Ruby gems?

Ruby gems is a package manager for Ruby libraries that has basic CRUD operations, dependency trees, and also supports asynchronous communication between gem libraries.

39. What is the highest level in the object model?


40. What are the different ways to invoke a method in Ruby?

We can use either the dot operator(.), send or the

obj =
puts obj.object_id

puts obj.send(:object_id)

puts obj.method(:object_id).call

41. What is the use of interpolation in Ruby?

Interpolation is an important process in Ruby where we can insert a string into a literal. We can interpolate a string into a literal by placing a hash (#) within curly brackets {}.

42. What is the use of global variable $ in Ruby?

When we declare a variable with prefix $, it becomes a global variable. This means we can access it anywhere within the application.

43. What is Rails?

Ruby on Rails is a simple and easy web application framework.

44. How to define regular expression in Ruby?

We can define regular expression in Ruby by using %r between arbitrary delimiters or slashes. A regular expression is a sequence of characters to find or match a particular sequence.

45. What are the looping controls in Ruby?

  • For loop
  • While loop
  • Until loop


Translate ยป