The dRuby Book

2.2 Design Principles of dRuby

I designed dRuby to extend Ruby method invocation over networks. dRuby is a library to implement distributed objects in Ruby.

dRuby has the following characteristics:

Let’s look at these concepts a little more closely.

images/d2block.png

Figure 10. The software layers. Observe where dRuby sits above the operating systems.

Pure Ruby

dRuby is a distributed object system purely targeted to Ruby. It sounds limiting, but this also means you can run dRuby in any environment where Ruby can run (see Figure 10, The software layers). This is very similar to Java RMI, which also can run anywhere Java can run.

Figure 11, An example of a system across multiple OSs shows the architecture of a complex system across different operating systems.

images/d2block2.png

Figure 11. An example of a system across multiple OSs. Div, Ring, Dip, and RWiki are applications that use dRuby.

dRuby is written purely in Ruby without using any C extension libraries—another bonus. Ruby comes with network, thread, and marshaling-related libraries as part of its standard library, so I was able to write everything in Ruby. The first version of dRuby had only 160 lines (the current dRuby has more than 1,700 lines including RDoc), and the core part of the library is still the same (see The First dRuby). The concise codebase of dRuby demonstrates how easily you can write a complex library by using just Ruby’s standard libraries.

Feels Like Ruby

I paid special attention to the compatibility between dRuby and Ruby. Many features of Ruby remain in dRuby, too.

Ruby is very dynamic. You don’t need to use inheritance most of the time because the variables of Ruby aren’t typed. Ruby looks up methods at execution time (method invocation time); these characteristics also apply to dRuby. dRuby doesn’t have type-in variables, and method searches are done at execution time. Because you don’t need to prepare the list of methods and their inheritance information, you don’t need to write IDL.

dRuby’s core mission isn’t about changing the behavior of Ruby, apart from extending Ruby method invocation across networks. With this functionality, you can have as much ease and fun programming in dRuby as you do with Ruby. For example, you can still use a block for method calls and use exceptions as well. Other multithreading synchronization methods, such as Mutex and Queue, are also available remotely, and you can use them to synchronize multiple processes.

Pass by Reference, Pass by Value

Having said all that, sometimes you need to know the difference between Ruby and dRuby.

In Ruby, objects are all exchanged by reference when you pass or receive method arguments, return values, or exceptions.

In dRuby, objects are exchanged either by reference or by the copy of the original value. When an object is passed by reference, the operation to the object will affect the original (like a normal Ruby object). However, when passed by value, the operation doesn’t affect the original, and the change stays within the process where the object is modified (see Figure 12, Passing by reference and passing by value).

images/d2pass.png

Figure 12. Passing by reference and passing by value

This difference doesn’t exist in Ruby, and you have to pay special attention to this when you program with dRuby.

If I really wanted to make dRuby look the same as Ruby, I could have designed dRuby to always pass by reference. However, remote object method invocation requires some network overhead, and doing small object operations all via RMI isn’t effective. It’s vital to pass the copy of the object in certain situations to increase your application’s performance. Also, if you keep passing by reference, you’ll never get the actual value from the remote server. Instead, you’ll be looping forever, trying to find out the object’s state.

It might sound a bit complicated, but don’t worry. You don’t have to specify whether you plan to pass by value or by reference—dRuby does it for you. Because dRuby automatically selects the reference method, you don’t have to write a special method for dRuby.

You’ll see more detail about automatically passing by value or by reference in Chapter 4, Pass by Reference, Pass by Value.