为什么数组 return 在 full_name 的枚举期间是一个随机布尔值,但在 ruby 控制台中不是? IRB

Why does the array return a random boolean during the enumeration of the full_name, but not in ruby console? irb

如果在 ruby 控制台中执行,为什么数组(在地图块内)仅 return [true, true, false]:x = [(rand 2)==1, (rand 5)==3, (rand 11)==6] 然后 x, x, x?

first = "stephanie"
last = "devenport"
middle = "Lbp"

full_name = "#{first} #{middle} #{last}"

puts "#{full_name}\s\s\s\s\s"
.rstrip.gsub(' ', '').split(//)
.map{ |char| [(rand 2)==1, (rand 5)==3, (rand 11)==6].any? ? "#{char + ['~', '%', '^', '#'].sample}" : "#{char.upcase + ['-', '_'].sample}" }.join.chop

运行 在航站楼内 returns => S_t~e~p~h#A_n~i^E_L_b~P-d~e^ v#E_n%p~o~R-T

x = [(rand 2)==1, (rand 5)==3, (rand 11)==6]

这将构造一个包含三个元素的数组并将其存储在x中。该数组中的值是随机的,但它们是在构造数组时确定的。每当您查看 x 时,它总是相同的三元素数组,因为已经进行了随机调用并且结果已存储在数组中。

问题是 x 只是指向内存中某些值的指针。你不能 "call" 它。要每次得到不同的结果,你需要 x 是一个方法

def x
  [(rand 2)==1, (rand 5)==3, (rand 11)==6]
end