string comparison

Discussion in 'Ruby' started by Boris Glawe, Dec 14, 2005.

  1. Boris Glawe

    Boris Glawe Guest

    Hi,

    I have a class containing a string as an instance variable and I have a local
    variable containing a string.

    I compare these two strings with '==' and with the .eql? method:

    ###################################

    obj.text = "hello world"
    localtext = "hello world"

    if obj.text == localtext
    then
    # do something
    end

    ###################################

    The condition never became true in my program. Thus I added some debug output.
    This is the the debugging code:

    if not obj.text == localtext
    then
    print "\"#{obj.text}\" is not equal \"localtext\"\n"
    end

    This is, what it gave me:

    "hello world" is not equal "hello world"

    It's not worth trying the example above, since it's not the actual code. The
    actual code in my project is much more complex, that's why I used this example
    to discribe my problem.

    Fact is that when the string comparison does not succeed I get an output like

    "hello world" is not equal "hello "world"

    Now it's your turn... What could cause this behaviour?

    thanks and greets

    Boris
    Boris Glawe, Dec 14, 2005
    #1
    1. Advertising

  2. Boris Glawe

    ako... Guest

    hello,

    the below code displays 'equal' on my machine:

    s0 = "hello world"
    s1 = "hello world"
    puts 'equal' if s0 == s1

    konstantin
    ako..., Dec 14, 2005
    #2
    1. Advertising

  3. You have real diff here:
    > "hello world" is not equal "hello "world"


    I tried this code.
    Behaves as expected, prints equal.

    class Obj
    attr_accessor :text
    end

    def test
    obj=Obj.new
    obj.text="world"
    localtext="world"
    if obj.text == localtext then
    print "equal\n"
    else
    print "not equal\n"
    end
    end

    test

    Isolate the problem, and wrap it in a test/unit. Then I can take a
    closer look.

    Christer

    --
    Posted via http://www.ruby-forum.com/.
    Christer Nilsson, Dec 14, 2005
    #3
  4. Boris Glawe

    Boris Glawe Guest

    Re: string comparison => problem solved

    > Isolate the problem, and wrap it in a test/unit. Then I can take a
    > closer look.
    >


    The problem was a whitespace, which I did not see. I actually wrapped the output
    in apostrophs in order to see whitespaces. Since I am programming with cgi, I
    see the output in the browser window, which prints whitespaces very small.

    Anyway thanks for the help!!

    greets Boris
    Boris Glawe, Dec 15, 2005
    #4
  5. Boris Glawe

    ako... Guest

    Re: string comparison => problem solved

    you can run your cgi script from a command line thus: 'ruby script.cgi'
    just set REQUEST_URI environment variable to set the url.

    konstantin
    ako..., Dec 15, 2005
    #5
  6. On 12/14/05, Boris Glawe <> wrote:
    > if not obj.text =3D=3D localtext
    > then
    > print "\"#{obj.text}\" is not equal \"localtext\"\n"
    > end
    >
    > This is, what it gave me:
    >
    > "hello world" is not equal "hello world"
    >


    Just to remove any precedence weirdness, does this behave differently
    from your failing code?
    puts %Q[#{obj.text} differs from #{localtext}] unless obj.text =3D=3D local=
    text
    Wilson Bilkovich, Dec 15, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    2
    Views:
    27,800
    Phil Hanna
    Sep 20, 2003
  2. grz02

    Java String comparison

    grz02, Feb 23, 2005, in forum: Java
    Replies:
    40
    Views:
    15,780
    nooobody
    Mar 5, 2005
  3. Replies:
    21
    Views:
    1,363
    Alex Vinokur
    Aug 18, 2007
  4. Smithers
    Replies:
    12
    Views:
    1,151
    Ben Voigt [C++ MVP]
    Jul 7, 2009
  5. Deepu
    Replies:
    1
    Views:
    213
    ccc31807
    Feb 7, 2011
Loading...

Share This Page