Important Ruby Questions and Links
4) List some features of Ruby?
Ruby has many features. Some of them are listed below.
- Object-oriented
- Flexible
- Dynamic typing and Duck typing
- Garbage collector
- Keyword arguments
8) Name some operators used in Ruby.
Operators are a symbol which is used to perform different operations.
- Unary operator
- Airthmetic operator
- Bitwise operator
- Logical operator
- Ternary operator
14) Explain Ruby if-else statement.
The Ruby if-else statement is used to test condition. There are various types of statement in Ruby.
- if statement
- if-else statement
- if-else-if (elsif) statement
- ternary statement
35) Explain module mixins in Ruby.
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.
40) What is concatenating string in Ruby. In how many ways you can create a concatenating string.
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.
41) What are freezing string in Ruby.
In most programming languages strings are immutable. It means that an existing string can't be modified, only a new string can be created out of them.
In Ruby, by default strings are not immutable. To make them immutable, freeze method can be used.
42) In how many ways you can compare Ruby string?
Ruby strings can be compared with three operators:
- With == operator : Returns true or false
- With eql? Operator : Returns true or false
- With casecmp method : Returns 0 if matched or 1 if not matched
45) How to access Ruby array elements? How many methods are used to access Ruby elements.?
Ruby array elements can be accessed using #[] method. You can pass one or more than one arguments or even a range of arguments.
Syntax:
Methods used to access Ruby elements:
- at method
- slice method
- fetch method
- first and last method
- take method
- drop method
46) In how many ways items can be added in an array in Ruby?
Ruby array elements can be added in different ways.
- push or <<
- unshift
- insert
47) In how many ways items can be removed from array in Ruby?
Ruby array elements can be removed in different ways.
- pop
- shift
- delete
- uniq
48) Explain Ruby hashes.
A Ruby hash is a collection of unique keys and their values. They are similar to arrays but array use integer as an index and hash use any object type. They are also called associative arrays, dictionaries or maps.
If a hash is accessed with a key that does not exist, the method will return nil.
49) How to create a new time instance in Ruby?
A new Time instance can be created with ::new. This will use your current system's time. Parts of time like year, month, day, hour, minute, etc can also be passed.
While creating a new time instance, you need to pass at least a year. If only year is passed, then time will default to January 1 of that year at 00:00:00 with current system time zone.
50) Explain Ruby ranges. What are the ways to define ranges?
Ruby range represents a set of values with a beginning and an end. They can be constructed using s..e and s...e literals or with ::new.
The ranges which has .. in them, run from beginning to end inclusively. The ranges which has ... in them, run exclusively the end value.
Ruby has a variety of ways to define ranges.
- Ranges as sequences
- Ranges as conditions
- Ranges as intervals
52) 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
54) 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.
57) 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.
60) 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.
Comments
Post a Comment