Initializing a Class Instance with a Unique Random Variable
While studying at Flatiron School, we use a test-based learning method. While this has a lot of benefits (which I may cover in a later blog post), it does limit creativity a little bit. As a specific example, in one lesson we were asked to build a BankAccount class. Initially, I thought that would involve creating a unique random number to use as an account number. However, this is not the case: the BankAccount instances were only initialized with a user and balance.
However, I was still curious about how, exactly, I could go about initializing a class instance with a number unique to itself. Because I think it’s important to think through problems, that’s what I will be exploring in the rest of the post.
To start, I want to set up the BankAccount class and initialize it with an owner, balance, and unique bank account number. Also, I want to keep track of all BankAccount instances.
class BankAccount def initialize(owner, balance = 0) @owner = owner @balance = balance @account_number = self.class.all << self end #of initialize attr_reader :account_number
attr_accessor :balance, :owner
@@all = [] def self.all @@all endend #of BankAccount class
So, above, we have set up an instance method “initialize” that accepts one mandatory argument (owner) and one optional argument(balance). If no balance is passed in, it will start with a balance of zero.
It’s worth noting at this point that many people who are new to initializing instances in a class believe that anything you set up must be passed in as an argument. This is not the case. As partially shown in my account_balance (left unfinished, as I haven’t decided yet how to pass in what I want), I am simply going to generate a number without manually passing it in.
So now I need to figure out how to create a random number, check it against existing instances, and put that into the initialize method. The first thing I’m going to do is generate a random number, like so.
number = (0..8).map{rand(0..9)}.join.to_s
What this is doing is, in places 0 through 8 (so nine total places), map out an array in which each index has a random number generated between and including 0 and 9. It will then join those numbers together and return them as a string. This result is assigned to the variable “number”.
Next, I need to figure out how to check that number against the account number of every single bank account class. If the number already exists, I need to generate a new number and run it again until we have a unique number.
I plan on trying to do this by, creating an if statement that forces the method to re-run itself if the number is already being used. This method will look like the following:
def unique_number number = self.generate_number if BankAccount.all.map{|bankaccount_instance| banckaccount_instance.account_number}.include?(number) self.unique_number else number
endend #of unique_number
I will then set the result of unique number equal to the account_number initialized with a new instance of the class. All together, the class looks like this:
class BankAccount def initialize(owner, balance = 0) @owner = owner @balance = balance @account_number = self.unique_number self.class.all << self end #of initialize
attr_reader :account_number attr_accessor :balance, :owner @@all = []
def self.all @@all end #of self.all def generate_number number = (0..8).map{rand(0..9)}.join.to_s end #of generate_number def unique_number number = self.generate_number if BankAccount.all.map{|bankaccount_instance|bankaccount_instance.account_number}.include?(number) self.unique_number else number
end #of if statement end #of unique_numberend #of BankAccount class
So, if I create a new BankAccount by inputting
account1 = BankAccount.new("Kimberlyn", 999999999)
where “Kimberlyn” is the owner and “999999999” is the balance(because I want a lot of money), the result is a new instance initialized with a unique, random number.
=> #<BankAccount:0x00005612c7929110 @owner="Kimberlyn", @balance=999999999, @account_number="108826941">
As a final note, the action I took in the instance method “unique_number” to call itself is called “recursion”. I have been informed by my Flatiron coach, after telling him what I planned to do for the method, that this concept is worth further study.