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.

January 18, 2007

Local variable assertion in Ruby

I'm just starting with Ruby, but one of the tricky parts of this language is that since it has no way to declare a variable, you can't tell it that you want a variable to be local. When you assign a value to a variable, you will be using a variable from a containing scope, if any such variable exists. And if no such variable exists, then you'll be creating a new one in the local scope. Block parameters also work this way, which is confusing.

I've attempted to create a workaround. My local() method asserts that the named variables do not yet exist, guaranteeing that any uses that follow will create local variables instead of clobbering existing variables. My code is in the extended entry. See the introductory comment for usage information.

I don't have enough Ruby experience yet to know if this is actually something useful, or just a curiosity. Creating this function was an interesting exercise for me, so I haven't looked yet to see if something like this already exists.

Read on for the code...

module Kernel
  # Assert that the named variables do not exist yet, 
  # so that they can be used as local variables in the block without
  # clobbering an existing variable
  #
  # This method expects any number of variable names as arguments.
  # The names may be specified as symbols or strings.
  # The method must be invoked with an associated block, although the
  # block may be empty. It uses the binding of the block with eval to check
  # whether the variable names are in use yet, and throws a NameError if
  # any of them are currently used.
  #
  # If the block associated with local expects no arguments, then this method
  # invokes it. The code within the block can safely use the symbols
  # passed to local.  If the block expects arguments, then local assumes
  # that the block is intended for the caller and just returns it.
  # 
  # Here are typical some uses of this method:
  # 
  #   local :x, :y  do     # Execute a block in which x and y are local vars
  #     data.each do |x|
  #       y = x*x
  #       puts y
  #     end
  #   end
  # 
  # Here's a way to use local where nested blocks are not needed:
  # 
  #   data.each &local(:x) {|x| puts x*x }
  # 
  # Here's a way to use it as an assertion with an empty block
  #
  #   local(:x, :y) {}  # Assert that x and y aren't in use yet.
  #   data.each do |x|  # Now go use those variables
  #     y = x*x
  #     puts y
  #   end
  #
  #
  def local(*syms, &block)
    syms.each do |sym|
      # First, see if the symbol itself is defined as a variable or method
      # XXX: do I also need to check for methods like x=?
      # XXX Would it be simpler or faster to do eval local_variables instead?
      value = eval("defined? #{sym.to_s}", block)
      # If it is not defined, then go on to the next symbol
      next if !value  
      # Otherwise, the symbol is in use, so raise an exception
      raise NameError.new("#{sym} is already a #{value}")
    end

    # If none of the symbols are in use, then we can proceed.
    # What we do next depends on the arity of the block, however.
    # If the block expects no arguments, then we just call it
    # If the block was declared with arguments, then it is not intended
    # for this method.  Instead, we return it so our caller can invoke it.
    if block.arity == 0 or block.arity == -1
      block.call
    else
      block
    end
  end
end

January 16, 2007

Desolation Row Revisited

Jan Lewis has written and recorded a great version of Desolation Row well suited to current events. You can hear and watch at http://www.youtube.com/watch?v=MQNBJpapIpQ.

Jan says he's a Java programmer, which allows me to make the weak case that this post is appropriate on this blog. New blog editorial policy: I'll consider linking to war protest songs written, recorded, or even just submitted by Java and JavaScript programmers and their ilk! :-)

If you're not a Bob Dylan fan, you may not be able to fully appreciate Jan's song, but it is still good. See my last post from December 2006 for the lyrics to the first verse of the original

January 03, 2007

Jude 1.03

I've relased a new minor version of Jude. This one fixes a bug that prevented it from reading certain class files in Java 6. Download Jude 1.03. As always, you need a license to use Jude. You can request an evaluation license or purchase a license.

If you've got no idea what I'm talking about, you can read about Jude.

Advertising
About
Store
Search
Google
Web this site
Archives
Syndicate

Powered by
Movable Type