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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:18:20+00:00 2026-06-05T19:18:20+00:00

class A attr_accessor :dab …. end Now I have an array of instances of

  • 0
class A
  attr_accessor :dab
  ....
end

Now I have an array of instances of A, say

arr = [A.new, A.new, A.new]

And now I want to set a value to all instances of class A present in the array arr. Is there a shortcut in ruby/rails to do this?

In addition, I do have A inherited from ActiveRecord::Base

And my actual need is:

A.find_all_by_some_condition.all.dabs = 2

So, all found objects will have dab set to 2.

Is there shortcut for this?

  • 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-05T19:18:22+00:00Added an answer on June 5, 2026 at 7:18 pm

    To get the items of class A from an array you can use select/find_all

    arr.select { |el| el.class == A } or arr.select { |el| A === el }

    To achieve your actual result though you are looking to assign a value to several objects, not their corresponding class. class A does not define the actual objects it just defines the blueprint that the objects use when getting created. So finding a way to assign a value of all instances of A is not what you are after (although I might have missed the point of what you were asking for)

    To assign a value to an array of object this works:

    A.find_all_by_some_condition.each { |a| a.dab = 2 }
    

    Perhaps you want to save them after that, now arr.each(&:save) might come in handy. Go look up the ampersand if you don’t know it already. Very useful.

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

Sidebar

Related Questions

I have a Model like this: class MyModel attr_accessor :size end Now i want
I have following code class User attr_accessor :name end u = User.new u.name =
I have a class like this class Foo attr_accessor :name end f = Foo.new
I have an object now: class Items attr_accessor :item_id, :name, :description, :rating def initialize(options
Let's say I have two objects: User and Race. class User attr_accessor :first_name attr_accessor
Can anybody say me, why that isn't working: class A attr_accessor :b end a
Say I have a like class like so: class User attr_accessor :name, :age def
I have an array of Mortgage objects class Mortgage attr_accessor :rate, :deposit def initialize(deposit,
If I have this class: class A attr_accessor :b,:c,:d end and this code: a
I have a 2D array containing many instances of a class. The class contains

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.