Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
What is << and <<= different in Ruby

I am a newbie about Ruby on Rails. I know << operator what it is doing but I am working with datatable and I have codes like these:

def data
    items.map do |donation|
      [].tap do |column|
        column << donation_path(donation)
        column <<= current_user.admin? ? link_to(donation.sender.name, admin_store_path(donation.sender)) : donation.sender.name 
      end
    end
  end

I tried to <<= in rails c and the result is:

irb(main):001:0> ar = []
=> []
irb(main):002:0> ar << 1
=> [1]
irb(main):003:0> ar <<= 1
=> [1, 1]
irb(main):004:0> ar <<= 2
=> [1, 1, 2]

I think <<= is similar with << but I have to be sure.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you use an operator op= in Ruby, the expression

x op= y

is equivalent to

x = x op y

In your case, it means that an

a <<= b

is equivalent to

a = a << b

but since a << b already modifies a, you gain nothing from using <<=.

NOTE: As was pointed out in the comments, this does not apply for the operator []: If you want to have an assignment version, you have to define []= explicitly.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!