# When {integer:0} \<\> 0?

**URL:** <https://www.ruby-forum.com/t/when-integer-0-0/186340>\
**Category:** Ruby\
**Created:** [April 5, 2010, 10:37am UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340 "2010-04-05T10:37:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 10:37am UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/1 "2010-04-05T10:37:04Z")

</div>

Greetings, folks!

This one will be so obvious to you all, but it has had me baffled for  
hours. (Script appended below.)

Please enlighten me! Thank you!

Richard F.

* * *

# the following is called with:

# do shell script “arch -i386 ruby ‘/Users/me/Desktop/Tests.rb’”

# Thank you, has!!

require “appscript”; include Appscript  
require ‘osax’; include OSAX

module Y; def self.z; 0; end; end  
osax.say(Y.z) #=\> “zero”

if p(Y.z): 0 # I assume this is the culprit!  
osax.say(Y.z) #=\> silence  
end

osax.set\_the\_clipboard\_to(Y.z) #=\> {integer:0}

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 10:47am UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/2 "2010-04-05T10:47:16Z")

</div>

Richard F. wrote:

> if p(Y.z): 0 # I assume this is the culprit!  
> osax.say(Y.z) #=\> silence  
> end

What are you trying to do here?

What you’ve written is equivalent to

if p(Y.z)  
0  
osax.say(Y.z)  
end

You haven’t defined a method called p, so you get the standard Kernel#p

“p x” is like “puts x.inspect” and returns nil, which is treated as  
false, so the code inside the if is not executed.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/3 "2010-04-05T16:35:15Z")

</div>

Brian C. wrote:

> Richard F. wrote:
> 
> > if p(Y.z): 0 # I assume this is the culprit!  
> > osax.say(Y.z) #=\> silence  
> > end
> 
> What are you trying to do here?

Thank you for the response, Brian!

(Y,z) is initially defined as 0:  
module Y; def self.z; 0; end; end

and both:  
osax.say(Y.z) #=\> “zero” and  
p(Y.z) #=\> {integer:0}  
validates that Y.z) = 0

What if statement do I have to write to get Ruby to recognize an  
existing value?

Thanks!

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/4 "2010-04-05T16:58:42Z")

</div>

Richard F. wrote:

> Thank you for the response, Brian!
> 
> (Y,z) is initially defined as 0:  
> module Y; def self.z; 0; end; end

That’s Y.z, not (Y,z)

> and both:  
> osax.say(Y.z) #=\> “zero” and  
> p(Y.z) #=\> {integer:0}  
> validates that Y.z) = 0

Yes. p(Y.z) shows you that Y.z is 0.

> What if statement do I have to write to get Ruby to recognize an  
> existing value?

Just the bare expression, Y.z

if Y.z == 0  
do\_something  
end

Or there’s a one-liner form:

do\_something if Y.z == 0

You could also write

if Y.z  
do\_something  
end

and this would work, because any value which is not nil or false is  
treated as true (so zero is true).

The colon is not a comparison operator, it is a (rarely-used) statement  
separator. It’s not what you want here.

e.g.

if true: puts “hello”; end

is same as

if true; puts “hello”; end

and

if true  
puts “hello”  
end

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/5 "2010-04-05T17:10:45Z")

</div>

Thank you, JesÃºs and Brian, that was what I needed!

Please forgive me for my vast ignorance. I have learned so many  
different Mac scripting languages over the years and all of them “have  
left the choir invisible and are pushing up daisies.” Thus I had no  
choice but to learn AppleScript. 😉

I am THRILLED that Ruby looks like a keeper! and I apologize again for  
my extreme Ruby “noobieness.”

Blessings and thank you!

Richard F.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/6 "2010-04-05T17:14:23Z")

</div>

On Mon, Apr 5, 2010 at 5:10 PM, Richard F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thank you, Jesús and Brian, that was what I needed!
> 
> Please forgive me for my vast ignorance. I have learned so many  
> different Mac scripting languages over the years and all of them “have  
> left the choir invisible and are pushing up daisies.” Thus I had no  
> choice but to learn AppleScript. 😉
> 
> I am THRILLED that Ruby looks like a keeper! and I apologize again for  
> my extreme Ruby “noobieness.”

No need to apologize. As I read sometimes in this list (I think it’s  
Robert K. who usually says this) we all started as newbies at some  
point 🙂

Jesus.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 4:46pm UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/7 "2010-04-05T16:46:53Z")

</div>

On Mon, Apr 5, 2010 at 4:35 PM, Richard F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (Y,z) is initially defined as 0:  
> module Y; def self.z; 0; end; end
> 
> and both:  
> osax.say(Y.z) #=\> “zero” and  
> p(Y.z) #=\> {integer:0}  
> validates that Y.z) = 0
> 
> What if statement do I have to write to get Ruby to recognize an  
> existing value?

if (Y.z == 0)  
osax.say(Y.z)  
end

you have two errors:

p(Y.z) as Brian explained is calling Kernel#p which always returns  
nil, and so using that return value in an if is not what you want. The  
second error is using : 0, when you are trying (I think) to compare  
the result of Y.z with 0. What follows the colon is the first  
statement in the if body, not a value to compare to.

Jesus.

---

<div class="post-metadata">

**Author:** ![richardfairhurst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@richardfairhurst](https://www.ruby-forum.com/u/richardfairhurst)\
**Post date:** [April 5, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/when-integer-0-0/186340/8 "2010-04-05T19:05:36Z")

</div>

On 04/05/2010 05:13 PM, Jesús Gabriel y Galán wrote:

> No need to apologize. As I read sometimes in this list (I think it’s  
> Robert K. who usually says this) we all started as newbies at some  
> point 🙂

I couldn’t agree more. 😉

Just one additional bit of information: the syntax with the colon is not  
supported any more in 1.9.1:

robert@fussel:~$ ruby -vc x.rb  
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]  
x.rb:7: warning: unused literal ignored  
Syntax OK  
robert@fussel:~$ ruby19 -vc x.rb  
ruby 1.9.1p376 (2009-12-07 revision 26041) [i686-linux]  
x.rb:7: syntax error, unexpected ‘:’, expecting keyword\_then or ‘;’ or  
‘\n’  
if p(Y.z): 0 # I assume this is the culprit!  
^  
x.rb:7: warning: unused literal ignored  
x.rb:9: syntax error, unexpected keyword\_end, expecting $end  
robert@fussel:~$ cat -n x.rb  
1 require “appscript”; include Appscript  
2 require ‘osax’; include OSAX  
3  
4 module Y; def self.z; 0; end; end  
5 osax.say(Y.z) #=\> “zero”  
6  
7 if p(Y.z): 0 # I assume this is the culprit!  
8 osax.say(Y.z) #=\> silence  
9 end  
10  
11 osax.set\_the\_clipboard\_to(Y.z) #=\> {integer:0}  
12  
robert@fussel:~$

Brian is absolutely right: the colon in this place is used so rarely  
that I even had forgotten about it. While we’re at it, let’s check  
another usage:

robert@fussel:~$ ruby -vce ‘case x; when 1: puts true else puts false  
end’  
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]  
Syntax OK  
robert@fussel:~$ ruby19 -vce ‘case x; when 1: puts true else puts false  
end’  
ruby 1.9.1p376 (2009-12-07 revision 26041) [i686-linux]  
-e:1: syntax error, unexpected ‘:’, expecting keyword\_then or ‘,’ or ‘;’  
or ‘\n’  
case x; when 1: puts true else puts false end  
^  
robert@fussel:~$

Aha, colon disappeared with “when” as well.

Kind regards

robert
