Ruby - Array element with yield value

Introduction

An array may include expressions that yield values.

Let's assume you have already created this method:

def hello 
  return "hello world" 
end 

You can now declare this array:

x = [1+2, hello, `dir`] 

Here, the first element is a mathematical expression that yields the integer 3.

The second is the string "hello world" returned by the method hello.

If you run this on Windows, the third array element will be a string containing a directory listing.

This is because `dir` is a back-quoted string, which is executed by the operating system.

The final "slot" in the array is filled with the value returned by the dir command.

Related Topic