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

  • Thread starter Richard Fairbanks
  • Start date
R

Richard Fairbanks

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

Brian Candler

Richard said:
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.
 
R

Richard Fairbanks

Brian said:
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!
 
J

Jesús Gabriel y Galán

Thank you for the response, Brian!

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

and both:
=A0 =A0osax.say(Y.z) =A0 =A0#=3D> "zero" and
=A0 =A0p(Y.z) =A0#=3D> {integer:0}
validates that Y.z) =3D 0

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

if (Y.z =3D=3D 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.
 
B

Brian Candler

Richard said:
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
 
R

Richard Fairbanks

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

Jesús Gabriel y Galán

Thank you, Jes=FAs 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. =A0;-)

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

Jesus.
 
R

Robert Klemme

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top