摘要:Putting code inside a module or class is a good way of separating it from other code.Ruby’s Math module is a good example—it defines constants such as Math::PI and Math::Eand methods such ...
阅读全文
摘要:RubyGems is a standardized packaging and installation framework for Ruby libraries andapplications. RubyGems makes it easy to locate, install, upgrade, and uninstall Ruby packages. Rake was initially...
阅读全文
摘要:Ruby comes with a debugger, which is conveniently built into the base system. You canrun the debugger by invoking the interpreter with the -r debug option, along with any otherRuby options and the nam...
阅读全文
摘要:RomanCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--1classRoman2MAX_ROMAN=49993definitialize(value)4ifvalue<=0||value>MAX_ROMAN5fail"Romanvalue...
阅读全文
摘要:break terminates the immediately enclosing loop; control resumes at the statement followingthe block. redo repeats current iteration of the loop from the start but without reevaluatingthe condition or...
阅读全文
摘要:Methods that return a boolean result (socalledpredicate methods) are often named with a trailing ?. Methods that are “dangerous,” or that modify their receiver, may be named with a traili...
阅读全文
摘要:[代码] In Ruby,these sequences are created using the . . and . . . range operators. The two-dot form createsan inclusive range, and the three-dot form creates a range that excludes the specified highva...
阅读全文
摘要:Modules are a way of grouping together methods, classes, and constants. Modules give youtwo major benefits: • Modules provide a namespace and prevent name clashes. • Modules support the ...
阅读全文
摘要:[代码]injectCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--1[1,3,5,7].inject(0){|sum,element|sum+element}#=>162[1,3,5,7].inject(1){|product,element|...
阅读全文
摘要:classCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--1classBookInStock2attr_reader:isbn3attr_accessor:price4definitialize(isbn,price)5@isbn=isbn6@pric...
阅读全文
摘要:Ruby uses a convention that may seem strange at first: the first characters of a name indicatehow the name is used. Local variables, method parameters, and method names should allstart with a lowercas...
阅读全文
摘要:[代码]Output: HELLO, I'M RAY hello, i'm ray Hello, i'm ray HELLO, i'm RAY hello, I'M rayThe upcase and downcase methods force all letters in the string to upper-or lowercase, respectively. T...
阅读全文
摘要:[代码]Output: dogs3dog2[代码]Output: dogs3dog2 quite1f.b.i1the1fella1that1man-about-town1he's1
阅读全文
摘要:If you're processing an ASCII document, then each byte corresponds to one character. Use String#each_byte to yield each byte of a string as a number, which you can turn into a one-character string:[代码...
阅读全文
摘要:To turn a symbol into a string, use Symbol#to_s, or Symbol#id2name, for which to_s is an alias.[代码]Output:a_symbol AnotherSymbol Yet another symbol!You usually reference a symbol by just typing ...
阅读全文
摘要:To see the ASCII code for a specific character as an integer, use the ? operator:[代码]Output: 97To see the integer value of a particular in a string, access it as though it were an element of an array:...
阅读全文
摘要:Ruby gives you a number of escaping mechanisms to refer to unprintable characters. By using one of these mechanisms within a double-quoted string, you can put any binary character into the string.[代码]...
阅读全文