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

Discussion in 'Ruby' started by Richard Fairbanks, Apr 5, 2010.

  1. 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}
     
    Richard Fairbanks, Apr 5, 2010
    #1
    1. Advertisements

  2. 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.
     
    Brian Candler, Apr 5, 2010
    #2
    1. Advertisements

  3. 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!
     
    Richard Fairbanks, Apr 5, 2010
    #3
  4. 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.
     
    Jesús Gabriel y Galán, Apr 5, 2010
    #4
  5. That's Y.z, not (Y,z)
    Yes. p(Y.z) shows you that Y.z is 0.
    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
     
    Brian Candler, Apr 5, 2010
    #5
  6. 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
     
    Richard Fairbanks, Apr 5, 2010
    #6
  7. 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.
     
    Jesús Gabriel y Galán, Apr 5, 2010
    #7
  8. 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:

    [email protected]:~$ ruby -vc x.rb
    ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
    x.rb:7: warning: unused literal ignored
    Syntax OK
    [email protected]:~$ 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
    [email protected]:~$ 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
    [email protected]:~$

    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:

    [email protected]:~$ 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
    [email protected]:~$ 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
    ^
    [email protected]:~$

    Aha, colon disappeared with "when" as well.

    Kind regards

    robert
     
    Robert Klemme, Apr 5, 2010
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.