Hi,
I have also updated the list at
http://www.glue.umd.edu/~billtj/ruby.html, which is the HTML version
of the list below.
I am sorry that our NNTP server was down since last Thursday morning
(10/3/02) and even until now they are still working on it. So I will
be “crippled” for a while and will not be able to follow the
discussions in almost real time (as the Google groups is about 30
minutes to one hour delay (?) as compared to the “tin” reader that I
use.) I have not seen many e-mails either, so probably our email
server is also affected.
In particular, it seems that the Google poster does not really support
80 column (or around that number) text, so some of the lines below are
wrapped around, which make them harder to read.
Regards,
Bill
···
=============================================================================
Things That Newcomers to Ruby Should Know
[1]Plain Text Format
* Resources:
+ HOME PAGE: [2]http://www.ruby-lang.org/en/
+ FAQ: [3]http://www.rubycentral.com/faq/
+ PITFALL:
[4]http://rwiki.jin.gr.jp/cgi-bin/rw-cgi.rb?cmd=view;name=pit
fall
+ ONLINE TUTORIAL/DOC/BOOK:
[5]http://www.rubycentral.com/book/
+ VERY USEFUL HINTS:
o “Programming Ruby” book by David Thomas and Andrew
Hunt,
“When Trouble Strikes” Chapter, "But It Doesn’t Work"
Section
o “The Ruby Way” book 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, you can set the environment
variable
RUBYOPT to ‘w’:
+ win32:
C:> set RUBYOPT=w
or
pressing F5 (to execute) 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 notation “Klass#method” in documentation is used only to
represent an “instance method” of an object of class Klass; it
is
not a Ruby syntax at all. A “class method” in documentation, on
the other hand, is usually represented as “Klass.method” (which
is
a valid Ruby syntax).
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. Example:
(0…2).each do |i|
puts "inside block: i = #{i}"
end
puts “outside block: i = #{i}” # >> undefined `i’
On the other hand,
i = 0
(0…2).each do |i|
puts "inside block: i = #{i}“
end
puts “outside block: i = #{i}” # >> 'outside block: i =
2’
and
j = 0
(0…2).each do |i|
j = i
end
puts “outside block: j = #{j}” # >> 'outside block: j =
2’
4. The String#[Fixnum] method does not return the “character”
(which
is a string of length one) at the Fixnum position, but instead
the
ASCII character code at the position (however, this may change
in
the future). Currently, to get the character itself, use
String#[Fixnum,1] instead.
Furthermore, there are additional ASCII conversion methods such
as
+ Integer#chr to convert from the ASCII code to the
character
65.chr # >> “A”
+ ?char to convert from the character to the ASCII code
?A # >> 65
5. 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.
6. In the case statement
case obj
when obj_1
…
when obj_k
…
it is the “===” method which is invoked, not the “==” method.
Also, the order is “obj_k === obj” and not “obj === obj_k”.
The reason for this order is so that the case statement can
"match” obj in more flexible ways. Three interesting cases are
when obj_k is either a Module/Class, a Regexp, or a Range:
+ The Module/Class class defines the “===” method as a test
whether obj is an instance of the module/class or its
descendants (“obj#kind_of? obj_k”).
+ The Regexp class defines the “===” method as a test
whether
obj matches the pattern (“obj =~ obj_k”).
+ The Range class defines the “===” method as a test whether
obj is an element of the range (“obj_k.include? obj”).
7. Array.new(2, Hash.new) # >> [{}, {}]
but the two array elements are identical objects, not
independent
hashes.
8. 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, for example, you use the "+“
operator to add the “numbers” without calling the conversion
methods, you will simply concatenate the strings.
An alternative is to use “scanf”
([6]http://www.rubyhacker.com/code/scanf).
9. 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.
10. 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.
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]
hsh = { arr => “object” }
s.upcase!
p hsh[arr] # >> nil (maybe not what was expected)
hsh.rehash
p hsh[arr] # >> “object"
12. In Ruby, only false and nil are considered as false in a
Boolean
expression. In particular, 0 (zero), “” or ‘’ (empty string),
[]
(empty array), and {} (empty hash) are all considered as true.
13. There is no standard, built-in deep copy in Ruby. One way to
achieve a similar effect is by serialization/marshalling.
Because
in Ruby everything is a reference, be careful when you want to
"copy” objects, especially for objects that contain other
objects
(such as arrays and hashes) and when the containment is more
than
one level deep.
14. “0…k” represents a Range object, while “[0…k]” represents an
array with a single element of type Range. For example, if
[0…2].each do |i|
puts "i = #{i}"
end
does not give what you expect, probably you should have written
(0…2).each do |i|
puts "i = #{i}"
end
or
0.upto(2) do |i|
puts "i = #{i}“
end
instead. Note also that Ruby does not have objects of type
"Tuple”
(which are immutable arrays) and parentheses are usually put
around a Range object for the purpose of precedence grouping
(as
the “dot” is stronger than the “dot dot” in the above example).
Things That Are Good to Know
1. In Ruby the "self assignment operator" goes beyond "+=, -=, *=,
/=, %=". In particular, operators such as "||=" also exist.
Please
see Table 18.4 in the “Programming Ruby” book for the complete
list.
2. If you do extensive numerical computations, you may consider
using
"Numerical Ruby"
([7]http://www.ir.isas.ac.jp/~masa/ruby/index-e.html).
3. If you have numerical arrays which consume a large amount of
memory and CPU time, you may consider using “NArray” which is
part
of the Numerical Ruby
([8]http://www.ir.isas.ac.jp/~masa/ruby/na/SPEC.en).
4. If you do integration between Ruby and C/C++, you may consider
using “SWIG” ([9]http://www.swig.org/).
5. If you do a lot of translation from Ruby to C, you may consider
using “rb2c”
([10]http://easter.kuee.kyoto-u.ac.jp/~hiwada/ruby/rb2c/).
6. If you want to speed up some parts of your Ruby code by writing
them in C, you may consider using “Inline”
([11]http://sourceforge.net/projects/rubyinline/).
* For comments on this list, you may e-mail me directly at
[12]billtj@glue.umd.edu.
_________________________________________________________________
Last updated: Oct 7, 2002.
This list itself is available at
[13]http://www.glue.umd.edu/~billtj/ruby.html.
The plain text format is produced from the HTML format with “lynx
-dump”.
_________________________________________________________________
References
- file://localhost/.automount/tulsi/home/tjb/Conf/ruby.txt
- http://www.ruby-lang.org/en/
- http://www.rubycentral.com/faq/
- http://rwiki.jin.gr.jp/cgi-bin/rw-cgi.rb?cmd=view;name=pitfall
- http://www.rubycentral.com/book/
- http://www.rubyhacker.com/code/scanf
- http://www.ir.isas.ac.jp/~masa/ruby/index-e.html
- http://www.ir.isas.ac.jp/~masa/ruby/na/SPEC.en
- http://www.swig.org/
- http://easter.kuee.kyoto-u.ac.jp/~hiwada/ruby/rb2c/
- http://sourceforge.net/projects/rubyinline/
- mailto:billtj@glue.umd.edu
- http://www.glue.umd.edu/~billtj/ruby.html