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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:58:45+00:00 2026-05-30T02:58:45+00:00

I’m looking for some advice on dynamic variables. Particularly if I’m making the best

  • 0

I’m looking for some advice on dynamic variables. Particularly if I’m making the best use of instance_variable_set or if there is a better way I should be looking at. Thanks in advance for any advice. Now for the code.

I’m creating a Blackjack game. And I have two value_creator methods that look like this:

class Blackjack

  def dealers_card_value_creator
    @card_value = @hit_dealer
      case
      when @card_value.match(/\d+/)
        @card_value = @card_value.match(/\d+/)[0].to_i
      when @card_value.match(/Ace/)
        if @dealers_hand >= 11
          @card_value = 1
        else
          @card_value = 11
        end
      when @card_value.match(/Jack|Queen|King/)
        @card_value = 10
      else
        puts "there was an error. please try again."
      end
    @hit_dealer = @card_value
    @dealers_hand = @dealers_hand + @hit_dealer
      if @dealers_hand > 21 then puts "Dealer went over 21. You won $#{@bet}. Nice!" 
        deal end
  end

def players_card_value_creator
    @card_value = @hit_player
    case
    when @card_value.match(/\d+/)
      @card_value = @card_value.match(/\d+/)[0].to_i
    when @card_value.match(/Ace/)
      if @players_hand >= 11
        @card_value = 1
      else
        @card_value = 11
      end
    when @card_value.match(/Jack|Queen|King/)
      @card_value = 10
    else
      puts "there was an error. please try again."
    end
    @hit_player = @card_value
    @players_hand = @players_hand + @hit_player
      if @players_hand > 21 then puts "You went over 21. You lost $#{@bet}. Try again." 
        deal end
  end
end

as you can see, they are basically the same. So I created this method to be used by both Dealers and Players.

def value_creator(string)
    hit = "@#{string}" #returns either @player or @dealer
    instance_variable_set(hit, "#{@hit_card}")
    @card_value = hit
    case
    when @card_value.match(/\d+/)
      @card_value = @card_value.match(/\d+/)[0].to_i
    when @card_value.match(/Ace/)
      if "@#{string}"_hand >= 11
        @card_value = 1
      else
        @card_value = 11
      end
    when @card_value.match(/Jack/)
      @card_value = 10
    when @card_value.match(/Queen/)
      @card_value = 10
    when @card_value.match(/King/)
      @card_value = 10
    else
      puts "there was an error. please try again."
    end
    "@#{string}" = @card_value
    "@#{string}"_hand = "@#{string}"_hand + "@#{string}"
  end

  def hit_dealer
    @hit_card = @shuffled_deck.pop
    value_creator("dealer")
  end

  def hit_player
    @hit_card = @shuffled_deck.pop
    value_creator("player")
  end

But when I try to call the value of "@#{string}" with hit_player it returns @player, not the value it was assigned.

Lots of code, but I hope it helps to give a better understanding of what I’m trying to do and will help better direct advice. Again, thanks in advance.

  • 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-30T02:58:47+00:00Added an answer on May 30, 2026 at 2:58 am

    The comment in

    hit = "@#{string}" #returns either @player or @dealer
    

    is not correct.
    The result is not @player or @dealer, it is the String '@player' or '@dealer'.

    You may do something like:

    def value_creator( variant )
      case variant 
        when :player
          hit = @player
        when :dealer
          hit = @dealer
        else
          raise ArgumentError
        end
        #....
    end
    def hit_dealer
      @hit_card = @shuffled_deck.pop
      value_creator(:dealer)
    end
    
    def hit_player
      @hit_card = @shuffled_deck.pop
      value_creator(:player)
    end
    

    Alternative: Use instance_variable_get to get the value.

      hit = instance_variable_get("@#{string}")
    

    Two other possibilities:

      hit = eval("@#{string}")
    

    (I don’t like it – eval is evil).

    And with send.

    You have to define two accessors first:

    attr_reader :player
    attr_reader :dealer
    

    Then you can use:

    def value_creator( string )
      hit = send( string )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker

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.