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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:39:55+00:00 2026-05-20T14:39:55+00:00

Take a look at this code please : fighter = [:str => 2, :dex

  • 0

Take a look at this code please :

fighter =  [:str => 2, :dex => 3, :mag => 3, :acc => 2, :hp => 2]
ranger =   [:str => 3, :dex => 2, :mag => 3, :acc => 2, :hp => 2]
magician = [:str => 3, :dex => 3, :mag => 2, :acc => 2, :hp => 2]
rate = Hash.new

if user.first_class == 'Fighter' then rate = fighter end
if user.first_class == 'Ranger' then rate = magician end
if user.first_class == 'Magician' then rate = ranger end

cost = Hash.new
cost[:str] = (user.strength_points + 1) * rate[:str]
cost[:dex] = (user.dexterity_points + 1) * rate[:dex]
cost[:mag] = (user.magic_points + 1) * rate[:mag]
cost[:acc] = (user.accuracy_points + 1) * rate[:acc]
cost[:hp]  = (user.health_points + 1) * rate[:hp]
cost

This reside in a function i’ve made, and i get a “Symbol as array index” when i execute it in Rails. I’m guessing that this is because of rate = fighter, rate = magician or rate = ranger possibility. Maybe i have to use clone on that.

My question is, what is the better way of selecting a hash based on an if comparison, doing the above thing ?

  • 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-20T14:39:56+00:00Added an answer on May 20, 2026 at 2:39 pm

    You are creating an Array with one entry which is a Hash:

    fighter =  [:str => 2, :dex => 3, :mag => 3, :acc => 2, :hp => 2]
    => [{:str=>2, :dex=>3, :mag=>3, :acc=>2, :hp=>2}]
    

    What you meant to do is just create a Hash:

    fighter = {:str => 2, :dex => 3, :mag => 3, :acc => 2, :hp => 2}
    => {:str=>2, :dex=>3, :mag=>3, :acc=>2, :hp=>2}
    

    Also, instead of three if statements, use a case statement:

    rate = case user.first_class
      when "ranger"
        ranger
      when "magician"
        magician
      when "fighter"
        fighter
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this code: template<class T> class A { class base
Could someone please take a look at this sloppy code and explain to me
Please take a look at this piece of code. I'm allocating one byte for
Please take a look at this below line of code in JSF <h:inputText id=name
Please take a look at this simple code: http://jsfiddle.net/kerp3/ The box has an inner
Please take a look at this piece of code I came up with. abstract
Please take a look at this code (thanks to user budwiser), it matches a
Please take a look at this XML code: <TableLayout android:id=@+id/tableLayout1 android:layout_width=fill_parent android:layout_height=fill_parent android:padding=10dip android:isScrollContainer=true
Please, take a look to this piece of code: <span class=something> <label>test1</label><br/> <label>test2</label><br/> <label>test3</label>
Please take a look at this code and run it: I'm getting very strange

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.