Ruby Interview Questions and Answers
Question - 81 : - How to open a file in Ruby?
Answer - 81 : -
A Ruby file can be created using different methods for reading, writing or both.
There are two methods to open a file in Ruby.
File.new method : Using this method a new file can be created for reading, writing or both.
File.open method : Using this method a new file object is created. That file object is assigned to a file.
Difference between both the methods is that File.open method can be associated with a block while File.new method can't.
Syntax:
f = File.new("fileName.rb")
Or,
File.open("fileName.rb", "mode") do |f|
Question - 82 : - Name different methods for IO console in Ruby?
Answer - 82 : -
The IO console provides different methods to interact with console. The class IO provides following basic methods:
- IO::console
- IO#raw#raw!
- IO#cooked
- IO#cooked!
- IO#getch
Question - 83 : - How many iterators are there in Ruby?
Answer - 83 : -
Following iterators are there in Ruby:
- each iterator
- times iterator
- upto and downto iterator
- step iterator
- each_line iterator
Question - 84 : - What are Ruby iterators?
Answer - 84 : -
Iterator is a concept used in object-oriented language. Iteration means doing one thing many times like a loop.
The loop method is the simplest iterator. They return all the elements from a collection, one after the other. Arrays and hashes come in the category of collection.
Question - 85 : - Explain what is Rails Active Record in Ruby on Rails?
Answer - 85 : -
Rails active record is the Object/Relational Mapping (ORM) layer supplied with Rails. It follows the standard ORM model as
- Table map to classes
- Rows map to objects
- Columns map to object attributes
Question - 86 : - Are instance methods public or private?
Answer - 86 : -
They are public by default. You can change their visibility using Module#private, Module#protected, or back again using Module#public.
Question - 87 : - Explain some differences between Ruby and Python
Answer - 87 : -
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.
Question - 88 : - How Many Types Of Associations Relationships Does A Model Have?
Answer - 88 : -
When you have more than one model in your rails application, you would need to create connection between those models. You can do this via associations. Active Record supports three types of associations:
- one-to-one: A one-to-one relationship exists when one item has exactly one of another item. For example, a person has exactly one birthday or a dog has exactly one owner.
- one-to-many: A one-to-many relationship exists when a single object can be a member of many other objects. For instance, one subject can have many books.
- many-to-many: A many-to-many relationship exists when the first object is related to one or more of a second object, and the second object is related to one or many of the first object.
You indicate these associations by adding declarations to your models:
has_one,
has_many,
belongs_to,
has_and_belongs_to_many
Question - 89 : - How do you remove nil values in array using ruby?
Answer - 89 : -
Array#compact removes nil values.
>> [nil,123,nil,"test"].compact
=> [123, "test"]
Question - 90 : - ention what is the difference in scope for these two variables: @@name and @name?
Answer - 90 : -
The difference in scope for these two variables is that:
- @@name is a class variable
- @name is an instance variable