Top 40 Ruby Interview Questions You Must Prepare 19.Mar.2024

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'.

To create migration command includes

C:rubyapplication>ruby script/generate migration table_name

Setter and getter methods in Ruby are generated with the attr_accessor method. attr_accessor is used to generate instance variables for data that’s not stored in your database column.

You can also take the long route and create them manually.

Define operator defines whether a passed expression is defined or not. If the expression is defined it returns the description string or null if the expression is not defined. If a variable is defined it gets initialized. If method_call is defined as true then method also gets defined. This is also the same case with super and yield.

  • Garbage collection is a process of reclaiming the memory space. Ruby deletes unallocated and unused objects automatically. This feature can be controlled by applying proper syntax and program through ruby.
  • Ruby performs garbage collection automatically. Ruby is an object oriented language and every object oriented language tends to allocate many objects during execution of the program.

Classes, variables, methods, constants and modules can be referred by ruby names. When you want to distinguish between various names you can specify that by the first character of the name. Some of the names are used as reserve words which should not be used for any other purpose. A name can be lowercase letter, upper case letter, number, or an underscore, make sure that you follow the name by name characters.

Ruby is a dynamic, open source programming language. It focuses on productivity and simplicity. The syntax is elegant which is natural to read and easy to write and use. The language is flexible, since it allows the developers alter freely all its parts. At will, one can remove essential parts and redefined. For instance , the symbol + can be redefined by the word ‘plus’ to one of the its classes – Numeric.

Ruby supports multiple programming paradigms, such as, functional, imperative, object oriented, relative. Dynamic memory management and dynamic type systems are additional features of Ruby.

If you declare one variable as global we can access any where, where as class variable visibility only in the class 

The function super is used to invoke the original method, searching of the method body starts in the super class of the object that was found to contain the original method. The following example depicts the same.

def url=(address)
super (address.blank? || address.starts_with?('http')) ? address : http://#{address}
end

The differences of the methods nil and false are:

  • nil cannot be a value, where as a false can be a value.
  • A method returns true or false in case of a predicate, other wise nil is returned.
  • false is a boolean data type, where as nil is not.
  • nil is an object for NilClass, where as false is an object of for FalseClass

A single-quoted strings don’t process ASCII escape codes, and they don’t do string interpolation.

Methods in ruby basically perform two functions, named operation and the code present in the class which does a specific function. In Ruby all your algorithms live in methods which inturn is present on objects. Ruby does not have any provision for functions. Code present in Ruby is always a method of some object. Behind the scenes ruby gives you the flexibility to work with methods as functions if you are considering working with other languages.

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.

This function calls a method and it can take any number of arguments and expr. Make sure that you put an asterisk or an ampersand before the expression. Last expr argument can be declared with a hash without any braces. If you want to increase the size of the array value then make sure that you put an asterisk before expression. “::” can be used to separate the class from methods.

A module cannot be subclassed or instantiated, and modules can implement mixins.

symbols are immutable and reusable, retaining the same object_id.

Be prepared to discuss the benefits of using symbols vs. strings, the effect on memory usage, and in which situations you would use one over the other.

Ruby class libraries comprise of variety of domains, such as thread programming, data types, various domains. It has additional libraries evolving day by day. The following are the domains which has relevant class libraries.

  • Text processing - File, String, Regexp for quick and clean text processing.
  • CGI Programming - There are supporting class library for CGI programming support like, data base interface, eRuby, mod_ruby for Apache, text processing classes.
  • Network programming – Various well-designed sockets are available in ruby for network programming.
  • GUI programming – Ruby/Tk and Ruby/Gtk are the classes for GUI programming
  • XML programming – UTF-8 text processing regular expression engine make XML programming very handy in ruby. .

Interpolation is a process of inserting a string into a literal. It is a very important process in Ruby. A string can be interpolated into a literal by placing a hash (#) within {} open and close brackets.

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.

  • Float class is used whenever the function changes constantly. It acts as a sub class of numeric. They represent real characters by making use of the native architecture of the double precision floating point.
  • Max is used whenever there is a huge need of Float.
  • Dig is used whenever you want to represent a float in decimal digits.

Following are the different environment variables present in the Ruby:

  • RUBYOPT
  • RUBYLIB
  • RUBYPATH
  • RUBYSHELL
  • RUBYLIB_PREFIX

The difference between Procs and Blocks,

  • Block is just the part of the syntax of a method while proc has the characteristics of a block.
  • Procs are objects, blocks are not.
  • At most one block can appear in an argument list.
  • Only block is not able to be stored into a variable while Proc can.

A class variable starts with an @@ sign which is immediately followed by upper or lower case letter. You can also put some name characters after the letters which stand to be a pure optional. A class variable can be shared among all the objects of a class. A single copy of a class variable exists for each and every given class.

To write a global variable you start the variable with a $ sign which should be followed by a name character. Ruby defines a number of global variables which also include other punctuation characters such as $_ and $-k.

Inserting a string into a literal is called as interpolation. Interpolation is a very important process in Ruby. Interpolation can be done by using only one way by embedding # within {}. A new name is referred to the copy of the original name.

Like all the modern languages Ruby supports many different operators to its credit. Most of the operators in Ruby are in fact methods due to the object oriented nature of the language. This feature gives Ruby an edge over its competitors. Semantics of these operators can be changed due to the object oriented nature of the language.

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.

Unlike other programming languages ruby deals with extremely large numbers it doesn’t have any barriers. There is no limit on the extent of limit of number usage. Ruby performs this function with two different classes they are fixnum and bignum. Fixnum represents easily managed small numbers and bignum represents big numbers. Ruby entirely handles the functioning of these two classes which leaves a programmer to concentrate on his arithmetic operations.

Ruby code blocks form an important part of ruby and are very fun to use. With the help of this feature you can place your code between do-end and you can associate them with method invocations and you can get an impression that they are like parameters. They may appear near to a source of the code and adjacent to a method call. The code is not executed during the program execution but it is executed when the context of its appearance is met or when it enters a method.

Ruby`s language is executed from the command line like most of the scripting languages. Programming and behavior language environment can be controlled from the interpreter itself. Some of the commands which are used are as follows –d, -h, -e prog, -v, -T, -r lib, etc.

Interpolation is a very important process in Ruby. Interpolation is the process of inserting a string into a literal. There is only one way in which you can interpolate a string into a literal by placing a Hash (#) within {} open and close brackets. This refers to a new name to by referring to the copy of the original method.

Free format – You can start writing from program from any line and column.
Case sensitive – The uppercase and lowercase letters are distinct.
Comments – Anything followed by an unquoted #, to the end of the line on which it appears, is ignored by the interpreter.
Statement delimiters- Multiple statements on one line must be separated by semicolons, but they are not required at the end of a line.

Ruby on Rails is a web application framework. It is written in Ruby. Compare to other frameworks for web application, the big deal is the way Ruby on Rails does. A web application finished in days instead of weeks, it is noticed by the community. Maintenance and/or extension of messy and hard web applications is flexible with ruby rails.

Ruby regular expression is a special sequence of characters that helps you match or find other strings. A regular expression literal is a pattern between arbitrary delimiters or slashes followed by %r.

  • Gem: A gem is a just ruby code. It is installed on a machine, and it’s available for all ruby applications running on that machine.
  • Plugin: Plugin is also ruby code, but it is installed in the application folder and only available for that specific application.

  • False is a boolean datatype 
  • Nil is not a data type 

There are four different types of variables they are local, instance, global, and class. Variables can be used in the program without any declaration and they can contain data of any type. A local variable contains lower case characters followed by name characters. Instance variable starts with a @ sign followed by name characters.

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.

Public, protected, and private.

  • Public methods can be called by all objects and subclasses of the class in which they are defined in.
  • Protected methods are only accessible to objects within the same class.
  • Private methods are only accessible within the same instance.

Ruby language can be ported to many platforms. Ruby programs can be ported to many platforms without any modification to the source code. This feature made the language very useful and highly used by many programmers worldwide. Some of the platforms used are DOS, UNIX, WINDOWS, etc.