Live Classes: Upskill your knowledge Now!
Chat NowPublished - Tue, 06 Dec 2022
A list of frequently asked Ruby or Ruby On Rails interview questions and answers are given below.
Ruby is a dynamic, reflective, general purpose, open source programming language that focuses on simplicity and productivity. Ruby has a mixed features of Perl, small talk, Eiffel, Ada and Lisp. Ruby was designed to create a new language which makes a balance with the functionality of Imperative languages.
For more information: Click here
Ruby is designed and developed by Yukihiro "martz" Matsumoto in mid 1990 in Japan.
For more information: Click here
Ruby is known as a language of flexibility because it facilitates its author to alter the programming elements. Some specific parts of the language can be removed or redefined. Ruby does not restrict the user. For example, to add two numbers, Ruby allows to use + sign or the word 'plus'. This alteration can be done with Ruby's built-in class Numeric.
Ruby has many features. Some of them are listed below.
For more information: Click here
Similarities:
Differences:
For more information: Click here
ruby -v
For more information: Click here
Ruby class libraries contain variety of domain such as thread programming, data types, various domains. Following is a list of domains which has relevant class libraries:
Operators are a symbol which is used to perform different operations.
For more information: Click here
RubyGems provides a standard format for distributing ruby programs and libraries. It works as a package manager for the Ruby programming language.
RubyGems is now a part of the standard library from Ruby version 1.9.
Ruby variables hold data which can be used later in a program. Each variable act as a memory and shas a different name.
There are four types of variables in Ruby:
For more information: Click here
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. |
Ruby data types represent type of data such as text, string, numbers, etc.
There are different data types in Ruby:
For more information: Click here
In Ruby, load and require both are used for loading the available code into the current code. In cases where loading the code required every time when changed or every times someone hits the URL, it is suggested to use 'load'.
It case of autoload, it is suggested to use 'require'.
The Ruby if-else statement is used to test condition. There are various types of statement in Ruby.
For more information: Click here
In Ruby, we use 'case' instead of 'switch' and 'when' instead of 'case'. The case statement matches one statement with multiple conditions just like a switch statement in other languages.
For more information: Click here
Ruby for loop iterates over a specific range of numbers. Hence, for loop is used if a program has fixed number of itrerations.
Ruby for loop will execute once for each element in expression.
For more information: Click here
Ruby while loop is used to iterate a program several times. If the number of iterations is not fixed
for a program, while loop is used.
For more information: Click here
Ruby do while loop iterates a part of program several times. In this, loop will execute at least once because do while condition is written at the end.
For more information: Click here
Ruby until loop runs until the given condition evaluates to true. It exits the loop when condition becomes true. It is opposite of the while loop.
For more information: Click here
Ruby break statement is used to terminate a loop. It is mostly used in while loop where value is printed till the condition is true.
For more information: Click here
Ruby next statement is used to skip loop's next iteration. Once the next statement is executed, no further iteration will be performed.
For more information: Click here
Ruby redo statement is used to repeat the current iteration of the loop. The redo statement is executed without evaluating loop's condition.
For more information: Click here
Ruby retry statement is used to repeat the whole loop iteration from the start.
For more information: Click here
Ruby comments are non-executable lines in a program. They do not take part in the execution of a program.
Single line comment syntax:
Multi line comment syntax:
For more information: Click here
Object is the default root of all Ruby objects. Ruby objects inherit from BasicObject which allows creating alternate object hierarchies.
For more information: Click here
Objects in Ruby are created by calling new method of the class. It is a unique type of method and predefined in Ruby library.
Syntax:
For more information: Click here
Each Ruby class is an instance of Ruby class. Classes in Ruby are first class objects. It always starts with a keyword class followed by the class name.
Syntax:
For more information: Click here
Ruby method prevent us from writing the same code in a program again and again. Ruby methods are similar to functions in other languages.
For more information: Click here
To use a Ruby method, we need to first define it. It is defined with def and end keyword.
Method name should always start with a lowercase letter.
Syntax:
For more information: Click here
Ruby code blocks are called closures in other programming languages. It consist of a group of codes which is always enclosed with braces or written between do...end.
For more information: Click here
A block is written in two ways:
Both are same and have the same functionality.
For more information: Click here
syntax:
For more information: Click here
The yield statement is used to call a block within a method with a value.
For more information: Click here
The &block is a way to pass a reference (instead of a local variable) to the block to a method.
Here, block word after the & is just a name for the reference, any other name can be used instead of this.
For more information: Click here
Ruby module is a collection of methods and constants. A module method may be instance method or module method. They are similar to classes as they hold a collection of methods, class definitions, constants and other modules. They are defined like classes. Objects or subclasses can not be created using modules. There is no module hierarchy of inheritance.
Modules basically serve two purposes:
Syntax:
Module name should start with a capital letter.
For more information: Click here
Ruby doesn't support multiple inheritance. Modules eliminate the need of multiple inheritance using mixin in Ruby.
A module doesn't have instances because it is not a class. However, a module can be included within a class.
When you include a module within a class, the class will have access to the methods of the module.
For more information: Click here
Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.
For more information: Click here
You can access Ruby string elements in different parts with the help of square brackets []. Within square brackets write the index or string.
For more information: Click here
Writing multiline string is very simple in Ruby language. We will show three ways to print multiline string.
For more information: Click here
The global variable is declared in Ruby that you can access it anywhere within the application because it has full scope in the application. The global variables are used in Ruby with $ prepend.
Ruby concatenating string implies creating one string from multiple strings. You can join more than one string to form a single string by concatenating them.
There are four ways to concatenate Ruby strings into single string:
For more information: Click here
In most programming languages strings are immutable. It means that an existing string can't be modified, only a new string can be created out of them.
In Ruby, by default strings are not immutable. To make them immutable, freeze method can be used.
For more information: Click here
Ruby strings can be compared with three operators:
For more information: Click here
Ruby class libraries contain variety of domain such as thread programming, data types, various domains. Following is a list of domains which has relevant class libraries:
Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.
Its indexing starts with 0. The negative index starts with -1 from the end of the array. For example, -1 indicates last element of the array and 0 indicates first element of the array.
A Ruby array is created in many ways.
For more information: Click here
Ruby array elements can be accessed using #[] method. You can pass one or more than one arguments or even a range of arguments.
Syntax:
Methods used to access Ruby elements:
For more information: Click here
Ruby array elements can be added in different ways.
For more information: Click here
Ruby array elements can be removed in different ways.
For more information: Click here
A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type. They are also called associative arrays, dictionaries or maps.
If a hash is accessed with a key that does not exist, the method will return nil.
For more information: Click here
A new Time instance can be created with ::new. This will use your current system's time. Parts of time like year, month, day, hour, minute, etc can also be passed.
While creating a new time instance, you need to pass at least a year. If only year is passed, then time will default to January 1 of that year at 00:00:00 with current system time zone.
For more information: Click here
Ruby range represents a set of values with a beginning and an end. They can be constructed using s..e and s...e literals or with ::new.
The ranges which has .. in them, run from beginning to end inclusively. The ranges which has ... in them, run exclusively the end value.
Ruby has a variety of ways to define ranges.
For more information: Click here
Iterator is a concept used in object-oriented language. Iteration means doing one thing many times like a loop.
The loop method is the simplest iterator. They return all the elements from a collection, one after the other. Arrays and hashes come in the category of collection.
For more information: Click here
Following iterators are there in Ruby:
For more information: Click here
The IO console provides different methods to interact with console. The class IO provides following basic methods:
For more information: Click here
A Ruby file can be created using different methods for reading, writing or both.
There are two methods to open a file in Ruby.
Difference between both the methods is that File.open method can be associated with a block while File.new method can't.
Syntax:
Or,
For more information: Click here
There are three different methods to read a file.
To return a single line, following syntax is used.
Syntax:
To return the whole file after the current position, following syntax is used.
Syntax:
To return file as an array of lines, following syntax is used.
Syntax:
Ruby class libraries contain a variety of domains like thread programming, data types, and various domains. It has additional libraries evolving day by day. The following are the domains which has relevant class libraries.
The sysread method is also used to read the content of a file. With the help of this method you can open a file in any mode.
For more information: Click here
Ruby files are renamed using rename method and deleted using delete mehtod.
To rename a file, following syntax is used.
Syntax:
To delete a file, following syntax is used.
Syntax:
For more information: Click here
To check whether a directory exists or not exists? Method is used.
Syntax:
For more information: Click here
Ruby exception is an object, an instance of the class Exception or descendent of that class. When something goes wrong, Ruby program throws an exceptional behavior. By default Ruby program terminates on throwing an exception.
For more information: Click here
Built-in subclasses of exception are as follows:
For more information: Click here
To handle exception, the code that raises exception is enclosed within begin-end block. Using rescue clauses we can state type of exceptions we want to handle.
For more information: Click here
Usaually in a rescue clause, the exception is captured and code resumes after begin block. Using retry statement, the rescue block code can be resumed from begin after capturing an exception.
Syntax:
For more information: Click here
The raise statement is used to raise an exception.
Syntax:
Or,
Or,
Or,
For more information: Click here
There is an ensure clause which guarantees some processing at the end of code. The ensure block always run whether an exception is raised or not. It is placed after last rescue clause and will always executed as the block terminates.
The ensure block will run at any case whether an exception arises, exception is rescued or code is terminated by uncaught exception.
Syntax:
Fri, 16 Jun 2023
Fri, 16 Jun 2023
Fri, 16 Jun 2023
Write a public review