Ruby Interview Questions and Answers
Question - 51 : - Explain How Can We Define Ruby Regular Expressions?
Answer - 51 : -
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.
Question - 52 : - List Out The Few Features Of Ruby?
Answer - 52 : -
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.
Question - 53 : - Mention What Is The Difference Between Procs And Blocks?
Answer - 53 : -
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.
Question - 54 : - Explain About The Command Line Options?
Answer - 54 : -
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.
Question - 55 : - What is concatenating string in Ruby. In how many ways you can create a concatenating string.
Answer - 55 : -
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.
Question - 56 : - How to write multiline string in Ruby.
Answer - 56 : -
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.
Question - 57 : - How to access Ruby strings elements in an application.
Answer - 57 : -
Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.
Question - 58 : - Explain module mixins in Ruby.
Answer - 58 : -
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.
Question - 59 : - Explain Ruby module.
Answer - 59 : -
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.
Question - 60 : - Explain ampersand parameter (&block) in Ruby.
Answer - 60 : -
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.