Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 823811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:00:18+00:00 2026-05-15T03:00:18+00:00

I was going through an example from the Programming in Ruby book: def fib_up_to(max)

  • 0

I was going through an example from the Programming in Ruby book:

def fib_up_to(max)
  i1, i2 = 1, 1 # parallel assignment (i1 = 1 and i2 = 1)
  while i1 <= max
    yield i1
    i1, i2 = i2, i1+i2
  end
end
fib_up_to(100) {|f| print f, " " }

This simply prints the Fibonacci numbers up to 100. That’s fine. When I replace the parallel assignment with:

i1 = i2
i2 = i1+i2

I am not getting the desired output.

Is it advisable to use parallel assignments? I come from a Java background and it feels really weird to see this type of assignment.

One more question is: Is parallel assignment an operator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:00:19+00:00Added an answer on May 15, 2026 at 3:00 am

    The problem with doing the assignment in 2 separate statements is that i2 = i1 + i2 will then use the new value of i1 rather than the previous value required to correctly generate the Fibonacci sequence.

    When you use parallel assignment all of the expressions on the right hand side are evaluated first and then assigned to the receiving variables on the left hand side.

    Parallel assignment isn’t a special operator. Essentially what is on the right hand side acts as an Array where if we list multiple variables on the left hand side then the array is unpacked and assigned into the respective variables.

    Here are some more examples:

    irb(main):020:0> a = 1, 2 # assign to a single variable
    => [1, 2]
    irb(main):021:0> a
    => [1, 2]
    irb(main):022:0> a, b = 1, 2 # unpack into separate variables
    => [1, 2]
    irb(main):023:0> a
    => 1
    irb(main):024:0> b
    => 2
    irb(main):025:0> a, b = [1, 2, 3] # 3 is 'lost' as no receiving variable
    => [1, 2, 3]
    irb(main):026:0> a
    => 1
    irb(main):027:0> b
    => 2
    irb(main):028:0> first, *rest = [1, 2, 3] # *rest consumes the remaining elements
    => [1, 2, 3]
    irb(main):029:0> first
    => 1
    irb(main):030:0> rest
    => [2, 3]
    

    It is a useful feature of ruby, for example it facilitates having methods that return multiple values e.g.

    def sum_and_difference(a, b)
      a + b, a - b
    end
    
    sum, difference = sum_and_difference 5, 3
    

    In Java the closest thing would be to have a method that returned int[] but if we wanted to return a string and a number we’d need to create a little POJO to act as struct for the return value or return Object[] and clutter the code up with casts. See this other question that I answered recently for a more practical example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 446k
  • Answers 446k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I understand what you're describing, this should do it:… May 15, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer I would use PHP's built-in path parsing functions to do… May 15, 2026 at 7:11 pm
  • Editorial Team
    Editorial Team added an answer ClassNotFoundException is more likely to be thrown (to your code)… May 15, 2026 at 7:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.