Home / Interview / Ruby :: General Questions

Interview :: Ruby

11) What is the difference between nil and false in Ruby?
nil false
nil cannot be a value. false can be a value.
nil is returned where there is no predicate. in case of a predicate, true or false is returned by a method.
nil is not a boolean data type. false is a boolean data type.
nil is an object of nilclass. false is an object of falseclass.
12)

Explain Ruby data types?

Ruby data types represent type of data such as text, string, numbers, etc.

There are different data types in Ruby:

  • Numbers
  • Strings
  • Symbols
  • Hashes
  • Arrays
  • Booleans

13) What is the use of load and require in Ruby?

In Ruby, load and require both are used for loading the available code into the current code. In cases where loading the code required every time when changed or every times someone hits the URL, it is suggested to use 'load'.

It case of autoload, it is suggested to use 'require'.

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

15)

Explain case statement in Ruby?

In Ruby, we use 'case' instead of 'switch' and 'when' instead of 'case'. The case statement matches one statement with multiple conditions just like a switch statement in other languages.

16)

Explain for loop in Ruby?

Ruby for loop iterates over a specific range of numbers. Hence, for loop is used if a program has fixed number of itrerations.

Ruby for loop will execute once for each element in expression.

17)

Explain while loop in Ruby?

Ruby while loop is used to iterate a program several times. If the number of iterations is not fixed

for a program, while loop is used.

18)

Explain do while loop in Ruby?

Ruby do while loop iterates a part of program several times. In this, loop will execute at least once because do while condition is written at the end.

19)

Explain until loop in Ruby?

Ruby until loop runs until the given condition evaluates to true. It exits the loop when condition becomes true. It is opposite of the while loop

20)

Explain break statement in Ruby?

Ruby break statement is used to terminate a loop. It is mostly used in while loop where value is printed till the condition is true.