Books & Tools Techniques

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

Completely updated for Ajax and Web 2.0

"A must-have reference"
Brendan Eich,
creator of JavaScript

Jude

Jude is my Java documentation browser. It combines Sun's definitive javadocs with the easy-to-use format of Java in a Nutshell, and tops it off with easy keyboard-based navigation and full-text searching.

Jude is available for free evaluation.

See the user's guide for more info

Java in a Nutshell

The 5th edition is now out, with complete coverage of Java 5.0!

It includes a fast-paced tutorial on the language, and a compact quick-reference for the core Java API.

Java Examples in a Nutshell

The 3rd edition, updated for Java 1.4

This edition has all-new coverage of the NIO and JavaSound APIs, completely rewritten Servlets and XML chapters, and coverage of new Java 1.4 features (assertions, logging, preferences, SSL, etc.) added througout. A great book for those who like to learn by example. 193 working examples: 21,900 lines of carefully commented code to learn from.

Java 1.5 Tiger: A Developer's Notebook

Amazon incorrectly credits me as the main author on this book. I'm actually the second author: really more of a consultant. This is a good book about all the language changes in the latest version of Java.

Effective Java

I didn't write this excellent book, but I wish I had.

Author Josh Bloch is probably best known for the collections classes in the java.util package. His experience and wisdom are apparent in this book. I learned from it and recommend it highly.

July 23, 2007

Ruby Structs for Immutable Types

Ruby's Struct class is a great way to define simple classes. For example, to define a simple Point class with getter and setter methods for x and y attributes, along with working ==, hash, and to_s methods, we can just write:

Point = Struct.new(:x,:y)

Struct was designed with mutable types in mind, but that doesn't mean that we are restricted to mutable types. Here's how we change the just-defined Point class to be immutable:

Point = Struct.new(:x, :y)  # Define mutable class
class Point                 # Open the class
  undef x=,y=,[]=           # Undefine mutator methods
end

Why do this for all of your Struct-based classes, however? Let's add a new factory method to Struct itself. If you want an immutable structure, use Struct.immutable instead of Struct.new:

def Struct.immutable(*args)  # Factory method for immutable classes
  Struct.new(*args).class_eval do # Define struct, then modify it
    undef []=                     # Undefine general mutator
    args.each do |sym|            # For each field of the struct
      mutator = :"#{sym.to_s}="   # Symbol for setter method
      remove_method mutator       # Undefine that method
    end
    self                          # Return the class
  end
end

Warning: I haven't actually tested this carefully, but it seems to work. Suggestions, corrections, etc., are welcome in the comments.

Advertising
About
Store
Search
Google
Web this site
Archives
Syndicate

Powered by
Movable Type