Ruby on Ralis

[Ruby] << 기호

sky1to 2020. 9. 1. 11:05

<< Operator

 

Array it appends the argument to the array

Example: [1,2,3] << 4
=> [1,2,3,4]

String it performs string concatenation

'Quora' << 'Digest'
=> 'QuoraDigest'

Set it adds the argument to the set

require 'set' ; Set[1,2] << 3
=> #<Set: {1, 2, 3}>

File it writes to the file descriptor

File.new('Ruby.txt','w') << "Hi"
=> Will create Ruby.txt file with ‘Hi ‘ content

For Integer it perform bitwise left-shift of the twos-complement

(0b1011 << 1).to_s(2)
=> 10110

>> Operator:

  1. For Integer it perform bitwise right-shift of the twos-complement
Example: (0b1011 << 1).to_s(2) => 101

[참고] https://www.quora.com/What-is-the-meaning-of-and-operators-in-Ruby