ruby运算符与铲子混淆(<<)和=,Concating数组
发布时间:2020-12-17 02:49:23 所属栏目:百科 来源:网络整理
导读:我已经读过 =.但我想我可能无法理解这些差异,因为我的预期代码不会输出我想要实现的内容. 回应 Ruby differences between += and to concatenate a string 我想把“猫”解读成一个字母/单词的数组 = [“c”,“ca”,“cat”,“a”,“at”,“t”] def helper(w
我已经读过<<<<<<< =.但我想我可能无法理解这些差异,因为我的预期代码不会输出我想要实现的内容. 回应
Ruby differences between += and << to concatenate a string
我想把“猫”解读成一个字母/单词的数组 def helper(word) words_array = [] idx = 0 while idx < word.length j = idx temp = "" while j < word.length **temp << word[j]** words_array << temp unless words_array.include?(temp) j += 1 end idx += 1 end p words_array end helper("cat") 我不明白为什么 解决方法
一个区别是因为<<在适当的地方工作它比=快一些.以下代码
require 'benchmark' a = '' b= '' puts Benchmark.measure { 100000.times { a << 'test' } } puts Benchmark.measure { 100000.times { b += 'test' } } 产量
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |