Online Book Reader

Home Category

Beyond Java - Bruce Tate [54]

By Root 702 0
That's a strong hint that Ruby is dynamically typed. Now, assign something else to n:

irb(main):013:0> n="fish"

=> "fish"

irb(main):014:0> n.class

=> String

Now, n has a string. We changed the type of the variable i. More accurately, the type in Ruby is bound to the object, but not the thing that contains it. So Ruby is dynamically typed. Let's try to do something strange:

irb(main):015:0> n+4

TypeError: cannot convert Fixnum into String

from (irb):15:in '+'

from (irb):15

Ruby won't break its typing rules by coercing a string to a Fixnum. That means Ruby is strongly typed.[*] You can get its length by invoking the size method on n:

irb(main):016:0> n.size

=> 4

How do you know what methods a string supports? Just ask one:

irb(main):017:0> n.methods

=> ["send", "%", "rindex", "between?", "reject", "[ ]=", "split", "<<",

"object_id", "strip", "size", "singleton_methods", "downcase", "gsub!",

...and so on...

So, String supports a whole bunch of methods. Try to count them with the size method. If you've always used statically typed languages, you will probably underestimate the benefits. You've read that dynamic typing lets you focus on the right part of the problem at the right time. It eases your refactoring burden, and reduces the amount of code that you have to write and maintain.

Conditionals


Ruby's conditionals will remind you more of C than Java. In Ruby, nil and false evaluate to false, and everything else (including true) means true. Read that sentence again. Unlike C, 0 is true. You should also notice that false and "false" are different. One is the Boolean constant for false, and one is a string. For example, puts "It's false." unless "false" returns nil, but puts "It's false." unless false will print It's false.

Ruby also has a few more conventions that you should know about. ? and ! are both valid in method names. By convention, methods ending in ? are tests. For example, nil? would test to see if a value is Nil. Methods ending in ! are potentially dangerous, because they have side effects. For example, a method called replace(in_string, sub_string, replacement) might return a string with the substring replaced, while replace!(in_string, sub_string, replacement) would modify the input string.

Like Java, Ruby has an if statement. Ruby also supports an unless statement that works the same way. You can use if or unless in block form, as you do in Java. You can also tack them onto the end of a line, to conditionally execute a single line of code. So, you can do something like this:

irb(main):099:0> def silence?(b)

irb(main):100:1> puts "SCREAM!" unless b

irb(main):101:1> end

=> nil

irb(main):106:0> silence? "False"

=> nil

irb(main):107:0> silence? "false"

=> nil

irb(main):108:0> silence? 0

=> nil

irb(main):109:0> silence? "quit kicking the cat"

=> nil

irb(main):110:0> silence? false

SCREAM!

=> nil

irb(main):111:0> silence? nil

SCREAM!

=> nil

Take a look at the silence? method. Ruby returns the value of the last statement, unless a method explicitly returns something. In this case, the statement puts "SCREAM!" unless b always returns nil. More importantly, the method prints SCREAM unless you pass it a true value.

Looping


Ruby has two conditional loops. You'll notice that many of Ruby's libraries help you by returning nil when they're done. If you're reading from standard input, you might do this:

irb(main):010:0> puts line while line=gets

one

one

two

two

^Z

=> nil

The loop continued until I entered the end-of-file character. Of course, you can also direct the input stream to a file. Plus you can use while at the beginning of a line, as long as you terminate it with an end:

irb(main):013:0> while line=gets

irb(main):014:1> puts line

irb(main):015:1> end

You've already seen Until, the other looping construct. It works in exactly the same way, but it will fire the loop while the expression is false. You'll also see a for loop later, but that's just syntactic sugar.

Ranges


Java programmers typically will specify a range using an arithmetic

Return Main Page Previous Page Next Page

®Online Book Reader