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 382163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:09:21+00:00 2026-05-12T15:09:21+00:00

I am in the midst of learning Ruby and thought I was clever with

  • 0

I am in the midst of learning Ruby and thought I was clever with the following piece of code:

[@start,@end].map!{ |time| time += operation == :add ? amount : -(amount) }

where @start, @end are two module level variables, operation can be one of :add or :sub, and amount is an float amount to adjust both @start and @end by.

Granted it only saves me a line of code, but why doesn’t this approach work, and how can I get something similar that does?

(My expected output is for @start/@end to be modified accordingly, however unit tests show that they stay at their original values.)

  • 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-12T15:09:21+00:00Added an answer on May 12, 2026 at 3:09 pm

    It’s important in Ruby to remember the distinction between variables and the objects they hold. Simply setting a variable will never change the object referenced by that variable. When you do a += b, it’s just shorthand for a = a + b. So you’re assigning a new value to the variable a, not changing the object that used to be there or changing any other references to that object. So changing the variable time doesn’t change @start.

    In order to assign to an instance variable, you need to actually assign to that instance variable. Here’s a way to do what you were looking for:

    operation = :+
    amount = 12
    @start, @end = [@start, @end].map {|time| time.send(operation, amount)}
    

    You’ll notice that we’re not faffing around with that :add and :sub business either — we can just pass the actual name of the message we want to send (I used + in this case, but it could be anything).

    If you had a big, dynamically generated list of ivars you wanted to set, it’s only a little bit more complicated. The only difference there is that need to get and set the ivars by name.

    ivars = [:@start, :@end, :@something_else]
    operation = :+
    amount = 12
    ivars.each {|ivar| instance_variable_set(ivar, instance_variable_get(ivar).send(operation, amount))}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am in the midst of a short thought experiment and would love some
I am in the midst of changing my JPA code around to make use
I am in the midst of learning Perl, and I have encountered a question.
Hi I am in the midst of Ruby on Rails tutorial. MY pages are
When I run the following code I'm getting an error that reads fig_match:rb:5:in '':
Currently I am in the midst of learning x86 assembly for fun, I'm love
I was in the midst of Ruby on Rails coding, having run several rails
I've been in the midst of creating a website, and I want to start
I'm in the midst of some refactoring some C# code and part of this
EDIT: This question is wrong, see end. I'm in the midst of integrating Facebook

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.