Reputation: 107
I'm sorry in advance i know a straight code dump is not a good way to ask a question but this is my first Ruby script and i can't even try to debug it because i'm getting weird errors. I am running my script using git and it's just straight broken.. What am i not understanding? Am i missing a gem install or something? Even a pointer to relevant reading would be greatly appreciated.
As is the error i recieve:
$ hello.rb
./hello.rb: line 8: class: command not found
./hello.rb: line 10: attr_accessor: command not found
./hello.rb: line 11: syntax error near unexpected token `('
./hello.rb: line 11: ` def initialize(name,cash,zip)'
and if i move my loop from the bottom to the top:
$ hello.rb
./hello.rb: line 6: syntax error near unexpected token `('
./hello.rb: line 6: `def main()'
hello.rb:
#
#
#
#
#
class Person
#Attribute accessor methods (accessor=writer+reader)
attr_accessor :name,:cash,:zip,:credit_card
def initialize(name,cash,zip)
@name=name
@cash=cash
@zip=zip
@credit_card=nil
end
end
#Ruby class naming convention: FirstLetterCapCamelCase
class Account
attr_accessor :account_name
#No writer attribute method for balance
attr_reader :balance
def initialize(Person)
time=Time.new
@key="Kudzu"
@Person=Person
@Atm=Atm.new
#seed the random number generator to get a new random set
#for each new instance of bank
srand(time.sec)
#Ruby naming convention for variables and methods is lowercase this_that
@name=Person.name
@balance=nil
@accounts=nil
# (#items, initial values) (ranndom range 100-999)
@credit_card = {"card_number"=>Array.new(4,1000+rand(9999)),"exp_month"=>time.month,"exp_year"=>time.year+4,"security_code"=>100+rand(999),"zip_code"=>Person.zip, "pin"=>@pin}
puts("Hello I'm glad you came in here's your new card #{@credit_card[card_number]}")
while(true)
puts("Please enter your pin so that i can initialize your card (1234) >> ")
temp = gets.chomp
if(temp.is_a?Numeric&&temp.lenth===4)
@pin = temp
break
puts("Pin must be a 4 digit number")
end
Person.credit_card=@credit_card
Atm.add_account(Person,credit_card,Base64::encode(key),Account)
puts("New account created, thanks for signing up #{account_name}!")
end
def withdraw(amount, Person, key)
if(Base64::decode(key)===@key)
if(@balance>=amount)
@balance-=amount
Person.cash+=amount
puts("Balance after withdraw: $#{this.balance}")
return true
else
puts("Insufficient Funds ):")
return false
end
else
puts("Access Denied")
return false
end
end
def deposit(amount, Person, key)
if(Base64::decode(key)===@key)
if(Person.cash>=amount)
# Negative amount
Person.cash+=amount-amount*2
@balance+=amount
puts("Balance after deposit: $#{this.balance}")
return true
else
puts("Show me the money!")
return false
end
else
puts("Access Denied")
return false
end
end
def transfer(ToAccount, ToPerson, FromPerson, amount, key)
if(Base64::decode(key)===@key)
if(withdraw(amount,FromPerson))
ToAccount.deposit(amount,ToPerson)
return "#{amount} has been transfered from #{account_name}'s account to #{ToAccount.name}'s account. Your remaining balance after this transaction is $#{@balance}"
end
else
puts("Access Denied")
return false
end
end
end
class Atm
@people={}
@accounts
@card={}
@Account
#Encoding is not encryption to do this: gem install encryptor
#I didn't take the time to learn how to implement encryption but..
#Neo "Do you know how to fly this thing?" Trinity "Not yet.."
@key="Kudzu"
#Ruby Convention is to use {} for a single line method body
def add_account(Person,credit_card,key,Account)
if(Base64::decode(key)===@key)
@people[credit_card]=Person
@accounts[credit_card]=Account
@Account=Account
else
puts("Access Denied")
return false
end
end
#Syntactical Sugar
def get_pin()
while(true)
puts("Ener Your Pin Number or (C)ancel >> ")
#The method chomp of the object gets returns user input without the trailing /n
temp=gets.chomp
if(temp===@card[pin])
@Person=@people[card]
req_action()
break
end
if(temp==="C")
break
end
puts("Didn't catch that try again")
end
def start_transaction(card)
get_pin()
end
private
def deposit()
puts("How much would you like to deposit >> ")
temp=gets.chomp
if(temp.is_a? Numeric)
@Account.deposit(temp, @Person, Base64::encode(key))
else
puts("Didn't catch that try again")
deposit()
end
end
def withdraw()
puts("How much would you like to withdraw >> ")
temp=gets.chomp
if(temp.is_a? Numeric)
@Account.deposit(temp, @Person, Base64::encode(key))
else
puts("Didn't catch that try again")
deposit()
end
end
def transfer()
puts("How much would you like to transfer >> ")
amount=gets.chomp
if(temp.is_a? Numeric)
puts("Card number of reciever (1234 5678 9123 4567)>> ")
temp=gets.chomp
#gsub = replace all
#\s+ = one more more white space characters
#m = multiline
#strip = remove white space from beg & end
#split will return the values split by given delimiter
temp=temp.gsub(/\s+/, ' ').strip.split(" ")
Person=@accounts[temp]
if(Person)
@Account.transfer()
deposit(@accounts[temp], @people[temp], @Person, amount, Base64::encode(key))
else
puts("We can only transfer to clients of the same bank (incorrect card number)")
transfer()
end
else
puts("Didn't catch that try again")
end
end
def req_action()
puts("Would you like to (W)ithdraw (D)eposit or (T)ransfer >> ")
temp=gets.chomp.upcase
temp=temp.upcase
#I wonder is it the Ruby convention to not have the when cases indented in the case conditional SublimeText seems to think so
case temp
when "W"
withdraw()
when "D"
deposit()
when "T"
Transfer()
else
puts("Didn't catch that try again")
req_action()
end
while(true)
puts("Leave Atm (y/n) Another Transaction >> ")
continue=true
temp=gets.chomp
temp=temp.upcase
case temp
when "Y"
puts("Returning Card")
continue=false
when "N"
req_action()
else
"Your fingers are too fat please try again"
end
if(!continue)break
end
end
end
while(true)
#return list of all instances of class Person in array people
$people=Person.all_offspring
$atm=Atm.all_offspring[0]
puts("would you like to go to the atm (a/r) or register a new account >> ")
temp=gets.chomp
temp=temp.upcase
case temp
when "A"
if(people)
while(true)
contine=true
puts("Who are you? #{people}")
temp=gets.chomp
for x in people
if(temp===people[x].name)
Person=people[x]
continue=false
end
end
if(!continue) break
end
end
$atm.start_transaction(Person.credit_card)
when "R"
puts("What is your name >> ")
name=gets.chomp
money=nil
zip=nil
loop do
done=false
puts("How much money do you have >> ")
temp=gets.chomp
if(temp.is_a?Numeric)
money=temp
done=true
break
else
"Not a number"
end
end
loop do
done=false
puts("What is your zip code >> ")
temp=gets.chomp
if(temp.is_a?Numeric)
if(temp.length===5)
money=temp
done=true
break
else
puts("invalid length")
end
else
puts("not a number")
end
end
else
"invalid input"
end
end
Upvotes: 1
Views: 1340
Reputation: 1682
put #!/usr/bin/env ruby
at the top of your script (assuming your using linux )
Upvotes: 1
Reputation: 369584
These are error messages from some shell, not Ruby. What you have there is a Ruby script. You need to run your Ruby script with Ruby. Your shell doesn't understand Ruby, it only understands its own language.
Upvotes: 2