Home / Interview / Ruby :: General Questions

Interview :: Ruby

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

43)

What are Ruby arrays and how they can be created?

Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.

Its indexing starts with 0. The negative index starts with -1 from the end of the array. For example, -1 indicates last element of the array and 0 indicates first element of the array.

A Ruby array is created in many ways.

  • Using literal constructor []
  • Using new class method

44)

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

45)

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

46)

In how many ways items can be removed from array in Ruby?

Ruby array elements can be removed in different ways.

  • pop
  • shift
  • delete
  • unique

47)

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.

48)

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.

49)

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

50)

What are Ruby iterators?

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.