Ruby中的<<和>>的作用详解

1.<<  和  >>代表左移或右移

puts  1<<2   

输出结果为:4 

代表1的二进制向左移动4位,即x<<y,x*2**y。平时用不到这种方法

2.字符串拼接

test = "you"

txt = "sb"

p test<<txt

输出结果为:yousb

3.数组添加元素

ar = Array.new

ar<<3

p ar

输出结果为:[3]

**字符串拼接和数组添加元素不能使用>>

原文地址:https://www.cnblogs.com/rose1jj/p/10220456.html