• +91 9723535972
  • info@interviewmaterial.com

Ruby Interview Questions and Answers

Ruby Interview Questions and Answers

Question - 71 : - Explain the use of ensure statement in Ruby?

Answer - 71 : -

There is an ensure clause which guarantees some processing at the end of code. The ensure block always run whether an exception is raised or not. It is placed after last rescue clause and will always executed as the block terminates.

The ensure block will run at any case whether an exception arises, exception is rescued or code is terminated by uncaught exception.

Syntax:

begin  
code..  
#..raise exception  
rescue  
#.. exception is rescued  
ensure  
#.. This code will always execute.  
end  

Question - 72 : - Explain raise statement in Ruby?

Answer - 72 : -

The raise statement is used to raise an exception.

Syntax:

raise  
Or,

raise "Error Message"  
Or,

raise ExceptionType, "Error Message"  
Or,

raise ExceptionType, "Error Message" condition  

Question - 73 : - Explain the use of retry statement in Ruby?

Answer - 73 : -

Usaually in a rescue clause, the exception is captured and code resumes after begin block. Using retry statement, the rescue block code can be resumed from begin after capturing an exception.

Syntax:

begin  
code....  
rescue  
# capture exceptions  
retry # program will run from the begin block  
end  

Question - 74 : - How an exception is handled in Ruby?

Answer - 74 : -

To handle exception, the code that raises exception is enclosed within begin-end block. Using rescue clauses we can state type of exceptions we want to handle.

Question - 75 : - What are some built-in Ruby class exceptions.

Answer - 75 : -

Built-in subclasses of exception are as follows:

  • NoMemoryError
  • ScriptError
  • SecurityError
  • SignalException

Question - 76 : - Explain Ruby exceptions.

Answer - 76 : -

Ruby exception is an object, an instance of the class Exception or descendent of that class. When something goes wrong, Ruby program throws an exceptional behavior. By default Ruby program terminates on throwing an exception.

Question - 77 : - How to check whether a directory exist or not in Ruby?

Answer - 77 : -

To check whether a directory exists or not exists? Method is used.

Syntax:

puts Dir.exists? "dirName"  

Question - 78 : - What is sysread method in Ruby?

Answer - 78 : -

The sysread method is also used to read the content of a file. With the help of this method you can open a file in any mode.

Question - 79 : - Explain class libraries in Ruby.

Answer - 79 : -

Ruby class libraries contain a variety of domains like thread programming, data types, and various domains. It has additional libraries evolving day by day. The following are the domains which has relevant class libraries.

  • Text processing: File, String, Regexp for quick and clean text processing.
  • CGI Programming: There are supporting class library for CGI programming support like, data base interface, eRuby, mod_ruby for Apache, text processing classes.
  • Network programming: Various well-designed sockets are available in ruby for network programming.
  • GUI programming: Ruby/Tk and Ruby/Gtk are the classes for GUI programming
  • XML programming: UTF-8 text processing regular expression engine make XML programming very handy in ruby.

Question - 80 : - How to read a file in Ruby?

Answer - 80 : -

There are three different methods to read a file.

To return a single line, following syntax is used.

Syntax:

f.gets  
code...  
To return the whole file after the current position, following syntax is used.

Syntax:

f.rea  
code...  
To return file as an array of lines, following syntax is used.

Syntax:

f.readlines  
[code...]  


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners