Home / Interview / Ruby :: General Questions

Interview :: Ruby

31)

In how many ways a block is written in Ruby?

A block is written in two ways:

  • Multi-line between do and end
  • Inline between braces {}

Both are same and have the same functionality.

syntax:

Module name should start with a capital letter.

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.

36)

Explain Ruby strings?

Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.

37)

How to access Ruby strings elements in an application?

You can access Ruby string elements in different parts with the help of square brackets []. Within square brackets write the index or string.

38)

How to write multiline string in Ruby.

Writing multiline string is very simple in Ruby language. We will show three ways to print multiline string.

  • String can be written within double quotes.
  • The % character is used and string is enclosed within / character.
  • In heredoc syntax, we use

39) What is the use of global variable $ in Ruby?

The global variable is declared in Ruby that you can access it anywhere within the application because it has full scope in the application. The global variables are used in Ruby with $ prepend.

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
  • Using concat method in between strings.