1) What is Ruby programming 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.
2) Who is the developer of Ruby?
Ruby is designed and developed by Yukihiro “martz” Matsumoto in mid 1990 in Japan.
3) List some features of Ruby?
Ruby has many features. Some of them are listed below.
Object-oriented
Flexible
Dynamic typing and Duck typing
Garbage collector
Keyword arguments
4) Why Ruby is known as a language of flexibility?
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.
5) Explain some differences between Ruby and Python.
Similarities:
High level language
Support multiple platforms
Use interactive prompt called irb
Server side scripting language
Differences:
Ruby is fully object oriented while Python is not.
Ruby supports EclipseIDE while Python supports multiple IDEs.
Ruby use Mixins while Python doesn’t.
Ruby supports blocks, procs and lambdas while Python doesn’t.
7) What are class libraries in Ruby?
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:
Text processing
CGI Programming
Network programming
GUI programming
XML programming
8) Name some operators used in Ruby.
Operators are a symbol which is used to perform different operations.
Unary operator
Airthmetic operator
Bitwise operator
Logical operator
Ternary operator
7) Write the command to get installed Ruby version in your system.
ruby -v
9) What is RubyGems in Ruby programming language?
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.
10) What are Ruby variables.
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:
Local variable
Class variable
Instance variable
Global variable
.
11) What is the difference between nil and false in Ruby?
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.
12) Explain Ruby data types.
Ruby data types represent type of data such as text, string, numbers, etc.
There are different data types in Ruby:
Numbers
Strings
Symbols
Hashes
Arrays
Booleans
.
13) What is the use of load and require in Ruby?
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’.
14) Explain Ruby if-else statement.
The Ruby if-else statement is used to test condition. There are various types of statement in Ruby.
if statement
if-else statement
if-else-if (elsif) statement
ternary statement
.
15) Explain case statement in Ruby.
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.
16) Explain for loop in Ruby.
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.
.
17) Explain while loop in Ruby.
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.
.
18) Explain do while loop in Ruby.
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.
.
19) Explain until loop in Ruby.
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.
.
20) Explain break statement in Ruby.
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.
.
21) Explain next statement in Ruby.
Ruby next statement is used to skip loop’s next iteration. Once the next statement is executed, no further iteration will be performed.
.
22) Explain redo statement in Ruby.
Ruby redo statement is used to repeat the current iteration of the loop. The redo statement is executed without evaluating loop’s condition.
.
23) Explain retry statement in Ruby.
Ruby retry statement is used to repeat the whole loop iteration from the start.
.
24) How will you comment in Ruby.
Ruby comments are non-executable lines in a program. They do not take part in the execution of a program.
Single line comment syntax:
This is single line comment.
Multi line comment syntax:
=begin
This
is
multi line
comment
=end
25) Explain Ruby object.
Object is the default root of all Ruby objects. Ruby objects inherit from BasicObject which allows creating alternate object hierarchies.
.
26) How to create Ruby object?
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:
objectName=className.new
27) Explain Ruby class.
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:
class ClassName
codes…
end
28) Define Ruby methods.
Ruby method prevent us from writing the same code in a program again and again. Ruby methods are similar to functions in other languages.
.
29) How to use Ruby methods.
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:
def methodName
code…
end
.
30) What are Ruby blocks.
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.
.
31) In how many ways a block is written in Ruby.
A block is written in two ways:
Multi-line between do and end
Inline between braces {}
Both are same and have the same functionality.
.
syntax:
block_name{
statement1
statement2
……….
}
.
32) What is yield statement in Ruby.
The yield statement is used to call a block within a method with a value.
.
33) Explain ampersand parameter (&block) in Ruby.
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.
.
34) Explain Ruby module.
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:
They act as namespace. They prevent the name clashes.
They allow the mixin facility to share functionality between classes.
Syntax:
module ModuleName
statement1
statement2
………..
end
Module name should start with a capital letter.
35) Explain module mixins in Ruby.
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.
.
36) Explain Ruby strings.
Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.
.
37) How to access Ruby strings elements in an application.
You can access Ruby string elements in different parts with the help of square brackets []. Within square brackets write the index or string.
38) What is the use of global variable $ in Ruby?
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.
39) What is concatenating string in Ruby. In how many ways you can create a concatenating string.
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:
Using plus sign in between strings.
Using a single space in between strings.
Using << sign in between strings.
Using concat method in between strings.
40) How to write multiline string in Ruby.
Writing multiline string is very simple in Ruby language. We will show three ways to print multiline string.
String can be written within double quotes.
The % character is used and string is enclosed within / character.
In heredoc syntax, we use << and string is enclosed within word STRING.
Leave a Reply