When is nil not false?

James Byrne wrote:

Can someone see what I cannot given this code?

class ForexCASource

  def initialize(source=nil)     xchg_source unless source     puts "this is source = #{source} #{source.class}"     puts "do we get here? why?"     xchg_source(source)   end

fx = ForexCASource.new

this is source = NilClass do we get here? why? TypeError: can't convert nil into String         from /usr/lib/ruby/1.8/open-uri.rb:32:in `open_uri_original_open'         from /usr/lib/ruby/1.8/open-uri.rb:32:in `open'         from /home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:58:in `xchg_source'         from /home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:52:in `initialize'         from (irb):1:in `new'         from (irb):1

xchg_source unless source

This is a one-liner that just doesn't call xchg_source if source is nil (which it is in this case).

All other lines will execute normally so:

xchg_source(source) will call xchg_source with a nil value. Since you haven't posted the code for xchg_source it's hard to tell what's happening in there.

did you mean:

      if !source         puts "this is source = #{source} #{source.class}"         puts "do we get here? why?"         xchg_source(source)       end

Cheers, Gary,