From: William Djaja Tjokroaminata Date: 2002-10-01T04:37:41+09:00 Subject: Re: Things That Newcomers to Ruby Should Know Hi, Because of my own "gotcha!" wrt Hash#rehash, I just updated the list by adding more resources in item 0) and my own gotcha as item 11). Regards, Bill Things That Newcomers to Ruby Should Know ========================================= 0) Resources: HOME PAGE : http://www.ruby-lang.org/en/ FAQ : http://www.rubycentral.com/faq/ PITFALL : http://rwiki.jin.gr.jp/cgi-bin/rw-cgi.rb?cmd=view;name=pitfall ONLINE TUTORIAL/DOC/BOOK: http://www.rubycentral.com/book/ VERY USEFUL HINTS : - "Programming Ruby" book by David Thomas and Andrew Hunt, "When Trouble Strikes" Chapter, "But It Doesn't Work" Section. - "The Ruby Way" by Hal Fulton, Chapter 1: "Ruby In Review" 1) Use "ruby -w" instead of simply "ruby" to get helpful warnings. If not invoking "ruby" directly, we can use instead: win32: C:\> set RUBYOPT=w or pressing F5 (to execute script) in the Scite editor will give you warnings (and F4 will position at problematic line). unix: sh# export RUBYOPT="w" or csh# setenv RUBYOPT "w" 2) The String#[Fixnum] method does not return the "character" (which is a string of length one) at the Fixnum position, but instead the character code at the position (however, this may change in the future). Currently, to get the character, use String#[Fixnum,1] instead. 3) Be aware of the lexical scoping interaction between local variables and block local variables. If a local variable is already defined before the block, then the block will use (and quite possibly) modify the local variable; in this case the block does not introduce a new scope. 4) In Ruby, there are two sets of logical operators: [!, &&, ||] and [not, and, or]. [!, &&, ||]'s precedence is higher than the assignments (=, %=, ~=, /=, etc.) while [not, and, or]'s precedence is lower. Also note that while &&'s precedence is higher than ||'s, the and's precedence is the same as the or's. 5) In the case statement case obj when obj1 .... it is the "===" method which is invoked, not the "==" method. Also, the order is "obj1 === obj" and not "obj === obj1". 6) Array.new(2, Hash.new) returns an array with two elements referencing the same, indentical hash, and not two independent hashes. 7) After reading data from a file and putting them into variables, the data type is really String. To convert them into numbers, use the "to_i" or "to_f" methods. If you use the "+" operator to add the "numbers" without calling the conversion methods, for example, you will simply concatenate the strings. An alternative is to use "scanf" (http://www.rubyhacker.com/code/scanf). 8) It is advisable not to write some white space before the opening '(' in a method call; else, Ruby with $VERBOSE set to true may give you a warning. 9) The "dot" for method call is the strongest operator. So for example, while in some other languages the number after the dot in a floating point number is optional, it is not in Ruby. For example, 1.e6 will try to call the method "e6" of the object 1 (which is a Fixnum). You have to write 1.0e6. However, notice that although the dot is the strongest operator, its precedence with respect to method name may be different with different Ruby versions. At least in Ruby 1.6.7, "puts (1..3).length" will give you a syntax error; you should write "puts((1..3).length)" instead. 10) The notation "Class#method" in a documentation is used only to represent an instance method of an object of class Class. It is not a Ruby syntax at all. A class method in a documentation, on the other hand, is usually represented as "Class.method" (which is a valid Ruby syntax). 11) Be careful when using "mutable" objects as hash keys. To get the expected result, call Hash#rehash before accessing the hash elements. Example: s = "mutable" arr = [s] hash = { arr => "object" } s.upcase! p hash[arr] # >> nil (maybe not what was expected) hash.rehash p hash[arr] # >> "object" 999) For comments, please e-mail me directly at billtj@glue.umd.edu. -------