RUBY programing interview question set 2/Ruby Interview Questions and Answers for Freshers & Experienced

Explain how can you declare a block in Ruby?

In Ruby, the code in the block is always enclosed within braces ({}). You can invoke a block by using “yield statement”.

Explain what is Polymorphic Association in Ruby on Rails?

Polymorphic Association allows an ActiveRecord object to be connected with Multiple ActiveRecord objects. A perfect example of Polymorphic Association is a social site where users can comment on anywhere whether it is a videos, photos, link, status updates etc. It would be not feasible if you have to create an individual comment like photos_comments, videos_comment and so on.

Define the background available in the feature file?

The background keyword found in the feature files is the basic steps to be executed before every test scenario. The steps are written under this keyword in order to be executed and in this way, they will be executed first before the commencement of any Cucumber test.

What are the various examples of behavior-driven tests written in plain text?

The following are the examples

1. Feature: XYZ page is needed to be visited in
2. Scenario: This is the final visit to
3. Given: Staying on the website,
4. When: Clicking on the XYZ page while being on the website,
5. Then: the ABC page is to be seen here.

Define Cucumber Dry Run?

All the feature files and the step definitions are all compiled together with the help of Cucumber Dry Run. It also shows the errors which appeared during the compilation process. Cucumber features - dry-run is one such example.

What are the meanings of support, hooks.rb, and env.rb?

Support related to Cucumber tools is put forward in the Support folder.

The execution of the scenarios of Cucumber tests requires the libraries to be loaded and this is used.

Before, before step, after, after step - these types of hooks are added with the help of hooks.rb.

Differentiate between Cucumber tool and Rspec?

The main differences between the Cucumber tool and Rspec are as follows. The cucumber tool is used for testing various software. It can also be used for integration and system tests. However, on the other hand, Rspec is used for the purpose of Unit Testing.

What is the meaning of the term profile in the Cucumber tool? What are the commands used to execute one?

For defining steps and defining certain specific features of Cucumber tests, Cucumber profiles are created.
Cucumber features - p, these are the commands used to execute.

What is the software required to run a test on the Cucumber tool?

The following are the software required to run a test on the Cucumber tool

1. Ruby and the development kit of Ruby
2. IDE like Active State
3. Cucumber
4. Watir for stimulation purposes of a browser
5. Anticon and RSpec

Define regular expressions?

A specific amount of testis expressed with the help of a pattern known as the regular expression. Mainly texts containing single characters are expressed with the help of this pattern.

Differentiate between Jbehave and Cucumber?

Cucumber tool and Jbehave have the same reason to be used by a user however they are built by the different framework -

1. Jbehave is based on Java whereas Cucumber is based on Ruby programming language.
2. Stories make the base of Jbehave whereas features make the base of the Cucumber tool.

Mention the log that has to be seen to report errors in Ruby Rails?

Rails will report errors from Apache in the log/Apache.log and errors from the Ruby code in log/development.log.

Explain what is Mixin in Rails?

Mixin in Ruby offers an alternative to multiple inheritances, using mixin modules can be imported inside other class.

Explain what is Cross-Site Request Forgery (CSRF) and how Rails is protected against it?

CSRF is a form of attack where hacker submits a page request on your behalf to a different website, causing damage or revealing your sensitive data. To protect from CSRF attacks, you have to add “protect_from_forgery” to your ApplicationController. This will cause Rails to require a CSRF token to process the request. CSRF token is given as a hidden field in every form created using Rails form builders.

Explain when self.up and self.down method is used?

When migrating to a new version, self.up method is used while self.down method is used to roll back my changes if needed.

Mention how you can create a controller for subject?

To create a controller for subject you can use the following command

ubylibrary> ruby script/generate controller subject

Mention what is the difference between String and Symbol?

They both act in the same way only they differ in their behaviors which are opposite to each other. The difference lies in the object_id, memory and process tune when they are used together. Symbol belongs to the category of immutable objects whereas Strings are considered as mutable objects.

Explain what is ORM (Object-Relationship-Model) in Rails?

ORM or Object Relationship Model in Rails indicate that your classes are mapped to the table in the database, and objects are directly mapped to the rows in the table.

Explain what is “Yield” in Ruby on Rails?

A Ruby method that receives a code block invokes it by calling it with the “Yield”.

What are the benefits of using BDD in selenium?

BDD that is the Behavior Driven Development is a very important programming language used in the development of software. This practice is implemented in Java with the help of using Cucumber - JVM which is also considered to be a mainstream tool. This framework of Cucumber is also used in Ruby on Rails other than Java.

The business also well as non-technical users can write various scenarios and features with the help of Cucumber JVM. These are written in Gherkin texts.

The step definition file is well supported by the feature file using which all the scenarios are properly executed. UI level tests alongside testing APIs can be done by combining with the Selenium WebDriver.

Define test harness?

The test harness performed various functions of the Cucumber test. It has the main responsibility of setting up test context. It also helps in the browser interaction and files like step definition are cleaned with the help of a test harness.

What is the meaning of the Steps in Cucumber tool?

The steps are sequences that help in describing how exactly the test scenario will take place. It also defines the preconditions of the test scenario. It consists of the following commands or keywords-

1. Given: Before starting the tests, all of its conditions are described by this.
2. When: The actions during Cucumber tests are described by this.
3. Then: This is the outcome of the actions which took place When.

Some other keywords found here are

1. And: logical representation of the Cucumber test
2. But: This is the same as And but is negative form.

What is the meaning of Scenario Outline available in the feature file?

Multiple Cucumber test scenarios are executed by using the Scenario Outline. The scenario outline uses various sets containing different test data. These raw provided with the help of using Examples that are found in the tabular structure.

Mention the main reasons behind using a simple programming language such as Gherkin?

The main reasons behind using slave simple programming language, Gherkin is

<> Gherkin helps with the documentation.
<> Tests are automated with the help of Gherkin.

How can any scenario in the feature file be written?

The Cucumber test scenarios are written with the help of a very simple language in the feature file. This language is known as Gherkin. This simple programming language is also business readable. The behavior and how the software developmental process works are all described with the help of this language.

It is specific to a particular domain of programming and all these are described from the perspective of a user. Hence, easy collaboration can be done by non-programmers too and can be easily read.

Define a route for a create action without using “resources”

1. With resources: resources :photos.

2. Without resources: post ‘/photos’, to: ‘photos#create’, as: :create_photo.

What is the difference between find, find_by, and where in ActiveRecord?

1. find: Takes a single argument and looks up the record where the primary key matches that argument.

2. find_by: Takes key/values and returns the first matching record.

3. where: Takes key/values and returns a collection of matching records. Or an empty collection if there are no matches.

What is the difference between class and instance variables?

Instance variables denoted by @, are associated with an instance of a class. Changing the value of an attribute on one instance has no effect on the variable for another instance.
Class variables denoted by @, are less intuitive. They are shared across all instances of a class. So, changing the variable on one instance affects that variable for all instances.

class Coffee
@@likes = 0
def like
@@likes += 1
def likes
puts @@likes
coffee_one =
coffee_two =
=> 2

What’s a scope?

A scope is ActiveRecord query logic that you can define inside a model and call elsewhere.

Defining a scope can be useful rather than duplicating the same logic in many places in the app.

# an example scope
class Post
scope :active_posts, -> { where(active:true) }

What is the difference between a class and a module?

A class has attributes and methods. You can create an instance of a class.

A module is just a collection of methods and constants, which you can mixin with another module or class.

What is the difference between load and require?

load runs another file, even if it’s already in memory.
require will only run another file once, no matter how many times you require it.

What is the splat operator?

Splat is used when you don’t want to specify the number of arguments passed to a method in advance. Ruby has two splat operators, the single splat and double splat.

The single splat works as you’d expect:

def do_sth(*input)
input.each {|x| puts x }
# => 3
# => 4
# => 5

What is the asset pipeline?

It’s a framework that prepares JavaScript and CSS for the browser.

What is Spring?

Spring is an application preloader. It keeps the application running in the background so booting is not required any time you run a migration or rake task.

What is your favorite Ruby gem?

One that every Rails developer knows, Devise. It makes setting up something complex like authentication a two-minutes job.

What do you dislike about Rails?

Machine learning libraries are poorly developed or non-existent.

What is Active Job?

Allows creating background jobs and queuing them on a variety of back ends like Delayed::Job or Sidekiq.
It’s typically used to execute code that doesn’t need to be executed in the main web thread. A common use case is sending notification emails to users.

What is content_for for?

It allows defining and rendering content in views. This is useful for defining content in one place and rendering it in many.

What is yield in Ruby?

yield accesses a block passed to a method. It’s typically used in layout files in a Rails application.
def puts_stuff
puts 'first line'
yield if block_given?
puts 'third line'
yield if block_given?
puts_stuff { puts 'its me' }
# => first line
# => its me
# => third line
# => its me

What is the difference between a proc and a lambda?

Both procs and lambdas are stored blocks but syntax and behavior differs slightly.
A lambda returns from itself but a proc returns from the method it’s inside.
def method_proc
thing = { return 1}
return 2
def method_lambda
thing = lambda { return 1}
return 2
puts method_proc # => 1
puts method_lambda # => 2
Notice that method_proc returns 1 because calling the proc ends execution within the method.

What is a block in Ruby?

A block is the code between two braces, {…}, or between do and end. You’re passing a block every time you call .each.
Blocks have their own scope and variables only defined inside a block are not accessible outside. But variables defined outside a block can be modified inside a block.
{|x| puts x} # a block

What is MVC?

MVC (Model-View-Controller) is a software design pattern that Rails is built around. It splits the handling of information into three pieces.
The model manages data and logic. The view displays information. The controller takes input and prepares data for a model or view

What is Rack?

Rack is an API sitting between the web server and Rails. It allows plugging in and swapping frameworks like Rails with Sinatra, or web servers like Unicorn with Puma.

When do we use “self” in Ruby?

Use self when defining and calling class methods.
In a class, self refers to the current class so it’s required when a class method calls another class method.
self.class.method is required when an instance calls a class method.

class Adder
# class method being called by class
puts,2,3) # => 6

What is ActiveRecord?

Active Record is an ORM (object-relational mapping) that maps models to database tables. It simplifies setting up an app because we no longer have to write SQL directly to load, save, or delete objects.

What logic goes into a helper?

Helper logic should support views only.
A good candidate for a helper is date formatting logic required in several different views.

How to declare a constructor on a Ruby class?

A constructor is defined via an initialize method which is called when a new instance of a class is initialized. Defining this method is not required. It’s often used to provide attribute values on new instances.

class Thing
attr_reader :name
def initialize(name)
@name = name
t ='dog')
puts # => 'dog

What frameworks have you used for backgrounding jobs?

Delayed::Job: Easy to set up and use. Queues are stored in a database table. If the same database is used for Delayed::Job and production, then a large number of jobs could turn the database into a bottleneck.

Sidekiq: Uses Redis to queue jobs. Redis is an in-memory data store so it’s very fast. Sidekiq adds complexity to infrastructure because Redis needs to be added.

Sucker Punch: Runs as a Ruby process and keeps all jobs in memory. Jobs are lost if the process crashes. Not recommended for critical tasks.

Is Ruby strongly or weakly typed?

Ruby is strongly typed. An error will be thrown if you try to calculate “hello” + 3.
In contract, JavaScript is weakly typed and would simply evaluate the same calculation to “hello3”.

Does Ruby allow multiple inheritances?

Ruby does not allow inheriting from more than one parent class, but it does allow module mixins with include and extend.

R4R Team
R4R provides Ruby Freshers questions and answers (Ruby Interview Questions and Answers) .The questions on website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,RUBY programing interview question set 2,Ruby Freshers & Experienced Interview Questions and Answers,Ruby Objetive choice questions and answers,Ruby Multiple choice questions and answers,Ruby objective, Ruby questions , Ruby answers,Ruby MCQs questions and answers Java, C ,C++, ASP, C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Ruby fresher interview questions ,Ruby Experienced interview questions,Ruby fresher interview questions and answers ,Ruby Experienced interview questions and answers,tricky Ruby queries for interview pdf,complex Ruby for practice with answers,Ruby for practice with answers You can search job and get offer latters by studing .learn in easy ways .