# When is nil not false?

**URL:** <https://www.ruby-forum.com/t/when-is-nil-not-false/165819>\
**Category:** Rails\
**Created:** [April 17, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819 "2009-04-17T20:47:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 17, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819/1 "2009-04-17T20:47:56Z")

</div>

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
> 
> >

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 17, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819/2 "2009-04-17T20:54:34Z")

</div>

James B. wrote:

> `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,

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 17, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819/3 "2009-04-17T20:55:10Z")

</div>

James B. wrote:

> Can someone see what I cannot given this code?

I need a return in front of the statement.

def initialize(source=nil)  
return xchg\_source unless source  
xchg\_source(source)  
end

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 18, 2009, 3:28am UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819/4 "2009-04-18T03:28:05Z")

</div>

James B. wrote:

> James B. wrote:
> 
> > Can someone see what I cannot given this code?
> 
> I need a return in front of the statement.
> 
> def initialize(source=nil)  
> return xchg\_source unless source  
> xchg\_source(source)  
> end

Why not just let xchg\_source deal with the nil then you just have:

def initialize(source=nil)  
xchg\_source(source)  
end

protected # assuming this is not part of the public interface  
def xchg\_source(source)  
if source  
# whatever you do if source is given  
else  
# whatever should happen if source is nil  
end  
end

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 18, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819/5 "2009-04-18T15:26:37Z")

</div>

What about:  
def initialize(source=nil)  
source ? xchg\_source(source) : xchg\_source  
end

Or if you prefer longer notation  
def initialize(source=nil)  
if source  
xchg\_source(source)  
else  
xchg\_source  
end

Or like in your example:  
def initialize(source=nil)  
return xchg\_source unless source  
xchg\_source(source)  
end

On Apr 17, 9:47 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [April 18, 2009, 3:32am UTC](https://www.ruby-forum.com/t/when-is-nil-not-false/165819/6 "2009-04-18T03:32:04Z")

</div>

Robert W. wrote:

> Why not just let xchg\_source deal with the nil then you just have:
> 
> def initialize(source=nil)  
> xchg\_source(source)  
> end
> 
> protected # assuming this is not part of the public interface

Ah, but it is public and if I pass a nil object to it then it fails, but  
it does not fail if I pass it nothing at all, or if I pass it a string,  
or a file object, etc. Odd, but there it is.
