When {integer:0} <> 0?

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 Fairbanks

···

----

# 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}
--
Posted via http://www.ruby-forum.com/.

Richard Fairbanks 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.

···

--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote:

Richard Fairbanks 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!

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Mon, Apr 5, 2010 at 4:35 PM, Richard Fairbanks <lists@f-p-i.com> wrote:

Brian Candler wrote:

Richard Fairbanks 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?

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

···

--
Posted via http://www.ruby-forum.com/\.

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. :wink:

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

Blessings and thank you!

Richard Fairbanks

···

--
Posted via http://www.ruby-forum.com/.

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

Jesus.

···

On Mon, Apr 5, 2010 at 5:10 PM, Richard Fairbanks <lists@f-p-i.com> 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. :wink:

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

I couldn't agree more. :wink:

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

···

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

On Mon, Apr 5, 2010 at 5:10 PM, Richard Fairbanks <lists@f-p-i.com> 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. :wink:

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 Klemme who usually says this) we all started as newbies at some
point :slight_smile:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/