What is the effect in Ruby when initializing with nil: x = String (nil)

What is the effect in Ruby when passing to the nilconstructor, as in:

s = String(nil)

or

a = Array(nil)

Does this mean that sora nilor what seither ais an uninhabited type object Stringor type Array?

+3
source share
1 answer

String(arg)calls to_son argand returns the result. nil.to_sreturns a new empty string. String(nil)therefore returns a new empty string.

Array(arg) to_ary, to_a arg, , ( [arg], ). NilClass to_ary, nil.to_a . Array(nil) .

, String(arg) Array(arg) . , Kernel. Ruby, new , . Array.new(2), 2.

+7

Source: https://habr.com/ru/post/1737626/


All Articles