Ruby 编程实践指南:避免常见错误和掌握语言精髓

需积分: 10 2 下载量 173 浏览量 更新于2024-07-28 收藏 2.93MB PDF 举报
"Ruby 语言基础知识和陷阱" Ruby 语言是一种动态的、面向对象的编程语言,它具有简洁的语法和易于使用的特点。然而,Ruby 语言也存在一些陷阱和复杂的特点,需要程序员小心避免。下面是 Ruby 语言的基础知识和陷阱: 一、Ruby 语言的简介 Ruby 语言是一种动态的、面向对象的编程语言,由 Yukihiro Matsumoto 于 1995 年创造。Ruby 语言的设计目标是提供一种易于使用、灵活、跨平台的编程语言。Ruby 语言的特点包括:简洁的语法、动态类型、面向对象编程、垃圾回收机制等。 二、Ruby 语言的陷阱 尽管 Ruby 语言的语法看起来很简单,但是它实际上非常复杂。Ruby 语言存在一些陷阱,需要程序员小心避免。这些陷阱包括: * Ruby 语言的语法虽然简单,但是它的实现却非常复杂。 Ruby 语言的语法糖和隐式转换规则可能会导致程序员的困惑。 * Ruby 语言的动态类型系统可能会导致类型错误和不兼容的问题。 * Ruby 语言的垃圾回收机制可能会导致性能问题和内存泄露。 三、Ruby 语言的版本 Ruby 语言有多个版本,包括 Ruby 1.8.x、Ruby 1.9.x 和 Ruby 2.0.x。Ruby 1.8.x 是目前最广泛使用的版本,但是 Ruby 1.9.x 和 Ruby 2.0.x 也在不断发展。每个版本都有其特点和改进的地方。 四、Ruby 语言的应用 Ruby 语言广泛应用于 Web 开发、脚本编程、系统管理等领域。Ruby 语言的popular框架包括 Ruby on Rails、 Sinatra 等。Ruby 语言也广泛应用于数据分析、科学计算、人工智能等领域。 五、总结 Ruby 语言是一种功能强大、灵活的编程语言,但是它也存在一些陷阱和复杂的特点。程序员需要小心避免这些陷阱,了解 Ruby 语言的特点和陷阱,才能更好地使用 Ruby 语言。

continue to use Java language, Add a class Borrower that extends User. The constructor of the Borrower class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor must throw a NotALenderException with the message “A new borrower cannot lend books.”. The borrower class does not have any instance variable. The moreBook method of the Borrower class increases the number of books borrowed by the borrower by the number of books given as argument to the method (so the books borrowed by the borrower becomes more positive!) For example, if a borrower currently borrows 10 books and moreBook(2) is called then the borrower borrows 12 books. It is fine for the moreBook method to be given a negative value as argument, which means the borrower then just returned some books. For example, if a borrower currently borrows 10 books and moreBook(-2) is called then the borrower borrows 8 books. However, a borrower cannot lend books, so the number of books borrowed by the borrower must always be positive or zero, never negative. If the argument given to the moreBook method is too negative and would change the book variable into a negative value, then the number of books borrowed by the borrower must not change and the moreBook method must throw a NotALenderException with the message “A borrower cannot lend XXX book(s).”, where XXX is replaced with the result of -(book + number). For example, if a borrower currently borrows 10 books and moreBook(-12) is called then the borrower still borrows 10 books and the method throws a NotALenderException with the message “A borrower cannot lend 2 book(s).”. Note: to simplify the project, do not worry about the setBook method. Change other classes and interfaces as necessary

2023-05-25 上传

Action 4: increasing the number of books of a given user. When the user of the software specifies action 4, your program must ask the user to type the name of a user, and a number of books, and the program then uses that number to increase the number of books lent or borrowed by the user. Then the program goes back to the main menu. For example: Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Anna Anna borrows -5 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Anna Enter the number of books: 2 Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Anna Anna borrows -7 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 10 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Bob Enter the number of books: 2 Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 12 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): If the name of the user is wrong, then an UnknownUserException exception will be thrown by the Library object. The code of the main method of your CLI class must catch this exception, print the error message from the exception object, and then it just goes back to printing the menu of actions (by just going back to the beginning of the while loop). For example (where 4, aaaa, and 2 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: aaaa Enter the number of books: 2 User aaaa unknown. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): Note that, even if a consumer is a borrower, the readPosInt method prevents the typed number of books from being negative. This means a borrower will never throw a NotALenderException. Nevertheless the code of the main method of your CLI class must handle this exception by printing the error message "BUG! This must never happen!" and immediately terminating the program using System.exit(1). For example (where 3, Bob, 4, Bob, and -15 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 12 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Bob Enter the number of books: -15 Positive integers only! Enter the number of books:

2023-05-24 上传