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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:14:06+00:00 2026-05-27T21:14:06+00:00

I am wondering if there is an elegant way to create variables using a

  • 0

I am wondering if there is an elegant way to create variables using a loop in ruby?

Say I want to put this in some sort of loop

def workhours
  @monday = blah
  @tuesday = brah
  @wednesday = bro
  @thursday = blap
  @friday = blagh
end

What I want to be able to do

def workhour_ideally
  days = [ "monday", "tuesday", "wednesday", "thursday", "friday" ]

  days.each do |smack|
    @"smack" = whatever
  end
end

Is this possible with ruby?

  • 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-27T21:14:07+00:00Added an answer on May 27, 2026 at 9:14 pm

    Yes it’s possible with Rails. No you shouldn’t do it. If I had to guess, I’d say this is a PHPism of some kind.

    What’s better is to use a Hash:

    @days = {
      :monday => blah,
      :tuesday => brah,
      # ...
      :friday => blagh
    }
    

    Then you can iterate over these as you wish. Referencing one of them is only slightly more typing: @days[:monday] instead of @monday.

    Using a proper data structure allows you to manipulate it, iterate over it, and offers all kinds of opportunities to test it thoroughly.

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

Sidebar

Related Questions

I was just wondering if there is an elegant way to set the maximum
I was wondering if there is a more elegant way to do IN() queries
I am wondering if there's an elegant solution for doing this in Java (besides
I'm wondering is there an elegant way to pass a locale in an ajax
I was wondering is there an elegant way of geting string[] from List<Tuple<int, int,
Wondering if there is an elegant way to listen for a function in JavaScript
Given something like this: var results = theElement.Element(Blah).Element(Whatever).Elements(Something); Is there an elegant way to
I was wondering if there a more elegant way of returning a value from
I was wondering if there's an abbreviation or a more elegant way of getting
I'm new to jQuery and wondering if there's an elegant way to select tags

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.