我很抱歉,我知道直接代码转储不是提问的好方法,但这是我的第一个Ruby脚本,我甚至无法调试它,因为我'我得到了奇怪的错误。我正在使用git运行我的脚本,它只是直接破坏了......我不理解什么?我错过了宝石安装或什么?即使是指向相关阅读的指针也会受到高度赞赏。
正如我收到的错误一样:
$ 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)'
如果我将循环从底部移动到顶部:
$ 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
答案 0 :(得分:0)
这些是来自某些shell的错误消息,而不是Ruby。你有什么Ruby脚本。您需要使用Ruby运行Ruby脚本。你的shell并不了解Ruby,它只能理解自己的语言。
答案 1 :(得分:0)
put #!/ usr / bin / env ruby
位于脚本顶部(假设您使用linux)