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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:36:03+00:00 2026-05-25T16:36:03+00:00

I am using Ruby on Rails 3.0.9 and RSpec 2. I am trying to

  • 0

I am using Ruby on Rails 3.0.9 and RSpec 2. I am trying to refactoring some spec file in the following way (in order to test with less code similar class object attribute values):

[
  :attribute_a,
  :attribute_b,
  :attribute_c
].each do |attr|
  before do
    # HERE I would like to set the "current" 'attr' related to the
    # class object instance 'attribute_< letter >' (read below for
    # more information) each time the iterator is called (note: all
    # following attributes are NOT attr_accesible - for that reason
    # I use the 'user.attribute_< letter >' in the 'before do'
    # statement)
    #
    # # Iteration 1
    #     user.attribute_a = 'a_value'
    # # No changes to 'user.attribute_b'
    # # No changes to 'user.attribute_c'
    #
    # # Iteration 2
    # # No changes to 'user.attribute_a'
    #     user.attribute_b = 'a_value'
    # # No changes to 'user.attribute_c'
    #
    # # Iteration 3
    # # No changes to 'user.attribute_a'
    # # No changes to 'user.attribute_b'
    #     user.attribute_c = 'a_value'

    # Maybe I should make something like the following but that, as well
    # as the 'send' method must be used, doesn't work (the below code-line
    # is just an example of what I would like to do).
    #
    # user.send(:"#{attr}") = 'a_value'
  end

  ...
end

How can I improve the above code so to accomplish what I aim (I refer to the user.send(:"#{attr}") = 'a_value' part in order to set “programmatically” – that is, set a different attribute value for each iteration made – each user attribute value to 'a_value')?

  • 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-25T16:36:03+00:00Added an answer on May 25, 2026 at 4:36 pm

    You should use .send and append an = to the method name to invoke the setter, passing the value as the second argument to send:

    [
      :attribute_a,
      :attribute_b,
      :attribute_c
    ].each do |attr|
      before do
        user.send("#{attr}=", 'a_value')
      end
    

    You’re effectively doing this:

    user.send('attribute_a=', 'a_value');
    

    Your syntax (user.send(:"#{attr}") = 'a_value') is wrong/weird for a couple reasons:

    • There’s no reason to convert :attr to a string and them immediately back to a symbol
    • You can’t assign a value to the return value of .send
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following the ruby on rails tutorial: http://railstutorial.org/chapters/static-pages#top I'm up to using rspec. Having
I am using Ruby on Rails 3.0.9, RSpec-rails 2 and FactoryGirl. I am trying
I am using Ruby on Rails 3.0.10, RSpec 2 and FactoryGirl. I am trying
I'm using rspec with ruby on rails for testing. Question - In a spec
I'm trying to write an app using Ruby on Rails and I'm trying to
How can I start using Rspec with an existing Ruby on Rails 2.3.5 project
After doing some stuff in Ruby on Rails with Cucumber, RSpec and Ruby BDD
I'm using RSpec for my Rails 3 tests and trying to use Spork. I
I'm using: Rails 3.0.7 and Rspec 2.5.0 via rvm When I run this spec
I'm trying to test a case in our Ruby on Rails system where we

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.