If you
want to add global functionality to Ruby, you should put it in the Object class.
There are several important methods on Object; following are the most used of these:
??? ==
??? class
??? clone
??? dup
??? equal?
??? hash
??? inspect
??? send
??? to_s
You can use the methods equal? and == to compare objects. clone and dup allow you to
make copies of an object. The class method returns the type of the object in question. hash
returns a hash code. inspect returns a String describing the object in detail, while to_s
returns a String representation that doesn??™t contain all detail about the object. send allows
you to call a named method on the object.
String
Strings are very useful and arguably the type most often used in Ruby. There are several ways
to create new String instances. Double quotes and single quotes are most common, but there
are also other versions. The difference mostly involves what kind of quoting is available inside
the String. With double quotes and %Q(), all quotes available in C Strings work, and you can
also interpolate any Ruby value. Single quotes and %q() only allow quoting backslash and the
single quote or end parenthesis, respectively.
APPENDIX A ?– RUBY FOR JAVA PROGRAMMERS 291
In a double quoted string, you can use the syntax #{} to interpolate any Ruby value you
want; you can also nest these statements and execute any Ruby code you want inside them:
"Hello, #{"world"}"
"Hello, number #{1+1}"
'Here, this: #{asdafgsd} doesn\'t do anything'
%Q(Interpolation, "is really good", he quipped #{'twice'})
%[Percent followed by parentheses, braces or brackets work like %Q]
%q(I can use any quote I want in here, like this: ', or this: ".
Pages:
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428