Latest Ruby And Rails Interview Questions Part – 2
1.How is visibility of methods changed in Ruby (Encapsulation)?
By applying the access modifier : Public ,Private and Protected access Modifier.
2.How is an enumerator iterator handled in Ruby?
Iterator is handled using keyword ‘each’ in ruby.
Ex: number=[11,12,13]
then we can use iterator as
number.each do |i|
puts i
end
Above prints the values of an array $no which is accomplished using iterator.
3.What is the scope of a local variable in Ruby and define it scope?
A new scope for a local variable is introduced in the toplevel,a class (module) definition,a method defintion.In a procedure block a new scope is introduced but you can access to a local variable outside the block.
The scope in a block is special because a local variable should be localized in Thread and Proc objects.
4.What are the object-oriented programming features supported by Ruby and how multiple inheritance supported in?
Classes,Objects,Inheritance,Singleton methods,polymorphism(accomplished by overriding and overloading) are some oo concepts supported by ruby.Multiple inheritance supported using Mixin concept.
5.What are the looping structures available in Ruby?
for..in
untill..end
while..end
do..end
6.What are the priority of operators available in Ruby?
Something that used in an expression to manipulate objects such as + (plus),- (minus),
* (multiply),and / (divide).You can also use operators to do comparisons,such as with <,>,and &.The priority is based on “BODMAS”
7.What is the Difference between Symbol and String?
Symbol are same like string but both behaviours is different based on object_id,memory and process time (cpu time) Strings are mutable ,Symbols are immutable.
8.Explain about ruby code blocks
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.
9.Explain about interpolation
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.
10.Explain about Float,Dig and Max
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.
11.How does ruby deal with extremely large numbers?
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.
12.Explain about Class variable and global variable
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.
13.Explain about variables
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.
14.Explain about normal method class
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.
15.Explain about methods
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 intum 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.
16.Explain about the defined operator
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.
17.Explain about operators in Ruby
Like all the modem 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.
18.Explain about environment variables present in ruby
Following are some of the environment variables used to control the behaviour programming of ruby.While programming ENV object lists some of the current variables.RUBYLIB path searches for libraries.Make sure that you separate each path with colons.RUBYOPT passes command line options to Ruby interpreter.There are many more which can be obtained by searching the huge pool of library.
19.Explain the difference between nil and false in ruby
False is a boolean datatype
Nil is not a data type .
20.What is the use of load and require in ruby?
Require() loads and processes the Ruby code from a separate file,including whatever classes,modules,methods,and constants are in that file into the current scope.Loads() performs the inclusion operation once,it reprocesses the code every time load is called.
21.Explain the concepts and capabilities of garbage collection feature of Ruby.
Ruby is an object oriented language and every object oriented language tends to allocate many objects during execution of the program.Ruby deletes unallocated and unused objects automatically.This feature can be controlled by applying proper syntax and program through ruby.
22.Describe class libraries in Ruby.
The Ruby standard library extends the foundation of the Ruby built-in library with classes and abstractions for a variety of programming needs,including network programming,operating system services,threads,and more.
These classes provide flexible capabilities at a high level of abstraction,giving you the ability to create powerful Ruby scripts useful in a variety of problem domains.
23.Explain about variables
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.
24.Explain about portability
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.
25.Explain about the command line options
Ruby’s language is executed from the command line like most of the scripting languages.Programming and behaviour 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.