Home / Interview / Ruby :: General Questions

Interview :: Ruby

51)

How many iterators are there in Ruby?

Following iterators are there in Ruby:

  • each iterator
  • times iterator
  • upto and downto iterator
  • step iterator
  • each_line iterator

52)

Name different methods for IO console in Ruby?

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

53)

How to open a file in Ruby?

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:

Or,

54) How to read a file in Ruby?

There are three different methods to read a file.

To return a single line, following syntax is used.

Syntax:

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

Syntax:

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

Syntax:

55)

Explain class libraries in Ruby?

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.

56)

What is sysread method in Ruby?

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.

57)

How will you rename and delete a file in Ruby?

Ruby files are renamed using rename method and deleted using delete mehtod.

To rename a file, following syntax is used.

Syntax:

To delete a file, following syntax is used.

Syntax:

58)

How to check whether a directory exist or not in Ruby?

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

Syntax:

59)

Explain Ruby exceptions?

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.

60)

What are some built-in Ruby class exceptions?

Built-in subclasses of exception are as follows:

  • NoMemoryError
  • ScriptError
  • SecurityError
  • SignalException