ruby - Why can't I Convert an inputed String to a Variable -


i have referred question on forum this: using string variable @ run time

this have:

def create_account   puts "username?"   account_name=gets.chomp   account_name_var=#account_name variable    #sets array variable   string_values = { }   some_string = [:some_string]       # parameter was, "hello"   string_values[some_string] = account_name   string_values                            # { 'hello' => 42 }   account_name_var = string_values[some_string]   account_name_var                              # 42    account_name_var=bankaccount.new(account_name, "open")   account_name_var=bankaccount.new(account_name, "open") end  create_account create_account # pablo=bankaccount.new("pablo", "open") # theo=bankaccount.new("theo", "open") make_transfer(name, name2, 10) 

but

: undefined local variable or method name main:object (nameerror) on line make_transfer(name, name2, 10)

.

so name not set variable when input name first username , name2 second username.

just return bankaccount instance method , assign variable:

def create_account   puts "username?"   account_name = gets.chomp   bankaccount.new(account_name, "open") end  account_1 = create_account account_2 = create_account make_transfer(account_1, account_2, 10) 

or, use hash store accounts name:

@accounts = {}  account = create_account @accounts[account.name] = account  account = create_account @accounts[account.name] = account  make_transfer(@accounts['pablo'], @accounts['theo'], 10) 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -