ruby - Accessing and checking a value within a hash -


looked around , not find proper answer problem facing. want access , check value within hash. code below...

class bank      class accountmaker         attr_accessor :account_number, :name, :balance, :pin          def initialize(account_number, name, balance, pin)             @account_number = account_number             @name = name             @balance = balance             @pin = pin         end     end      attr_accessor :int_account_number, :int_account_pin      def initialize         @accounts = {}         @int_account_number = int_account_number         @int_account_pin = int_account_pin     end      def add_account(account_number, name, balance, pin)         @accounts[account_number] = accountmaker.new(account_number, name, balance, pin)     end      def login_screen          def account_number_login             puts "please enter 7 digit account number."             account_number = gets.chomp             int_account_number = account_number.to_i              if @accounts.has_key?(int_account_number) , (/^\w{7}$/ === account_number)                 thank_you_msg()                 pin_login(int_account_number)             else                 error_msg()                 account_number_login()             end         end          def pin_login(int_account_number)             puts "please enter 4 digit pin."             account_pin = gets.chomp             int_account_pin = account_pin.to_i #may have use later              #puts int_account_number, int_account_pin #used check if variables come through              if (what should go here?) == int_account_pin #(/^\d{4}$/ === account_pin)                  thank_you_msg                 main_menu()             else                 error_msg()                 pin_login(int_account_number)             end         end          account_number_login()     end      def main_menu         end  end 

the question have how access hash value of pin user inputed account number(key) , check if matches user entry? hash contains value under :pin having hardest time trying access , compare it.

the question have how access hash value of pin user inputed account number(key) , check if matches user entry? hash contains value under :pin having hardest time trying access , compare it.

like this:

@accounts[int_account_number].pin 

the fragment:

@accounts[int_account_number] 

is accountmaker instance. and, accountmaker instance has getter method named pin() (as setter method named pin=()):

                                          declared right there!                                                    | class accountmaker                                 v   attr_accessor :account_number, :name, :balance, :pin   

but, code needs reorganized--starting indenting: ruby indenting 2 spaces--not 1 space, not 4 spaces. can indent ruby code 7 spaces if want--but don't expect when post on public forum. so, first task re-indent code 2 spaces.

next, rid of nested class: move top level , rename account. class names not verbs--they nouns. methods verbs.

then, rid of nested def's--nesting def doesn't work think does. rather, def's should defined @ top level of bank class.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -