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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:38:59+00:00 2026-06-03T00:38:59+00:00

I am looping through a list of objects and changing some of the values.

  • 0

I am looping through a list of objects and changing some of the values. When I output the value into the logger, I see the changed values, but when then on the resulting page, the changes do not get saved.

Here is my loop:

@dis.each do |d|
  temp = d.notes.inspect

  #Now check the length of the temp variable
  temp.length > 25 ? temp = temp[0,25] :  nil

  d.notes = temp
end

How do I change it so that the new values of temp get saved in the @dis object?

Thanks!

  • 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-06-03T00:39:01+00:00Added an answer on June 3, 2026 at 12:39 am

    You can get the result you want using collect! or map! to modify the
    array in-place:

    https://stackoverflow.com/a/5646754/643500

    x = %w(hello there world)
    x.collect! { |element|
      (element == "hello") ? "hi" : element
    }
    puts x
    

    Edit:

    So for your code it will look like

    @dis.collect! do |d|
      temp = d.notes.inspect
    
      #Now check the length of the temp variable
      temp.length > 25 ? temp = temp[0,25] : temp = nil
    
      d.notes = temp
    end
    

    Edit:

    Full code that worked here. Make sure you have :notes with getters and setters. Read about cattr_accessor, attr_accessor and attr_accessible

    class TestClass
      @note
      def initialize note
        @note = note
      end
      def get_note
        @note
      end
      def set_note note
        @note = note
      end
    end
    
    @dis = Array.new
    @dis << TestClass.new("yo yo")
    @dis << TestClass.new("1 2 3 4 5 6 7 8 9 10 6 7 8 9 10 6")
    @dis << TestClass.new("a b c")
    
    @dis.collect! do |d|
      temp = d.get_note.inspect
    
      #Now check the length of the temp variable
      d.get_note.inspect.length > 25 ? d.set_note(temp[0,25]) : d.set_note(nil)
    
    end
    
    
    puts "#{@dis}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looping through each id retrieve value of objects in 1 dimentional array and 2
I've got a repeater looping through a list of objects that are of different
So I am looping through some objects and initializing a Dictionary> object. So first
I have a foreach looping through a List of objects in an ASP.NET MVC/Razor
For some reason I can push JSON objects to my array list, but when
I am looping through a list for a spellchecker in vb.net (using vs 2010).
I am looping through a list of samaccountnames and performing several actions: # Disabling
I'm looping through a List of elements with foreach, like this: foreach (Type name
I've been looping through the contents of a list in the simple fashion to
i usally work with Json objects on my asp.net mvc by looping through the

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.