Search code examples
stringsmalltalksqueakvisualworks

Smalltalk - Compare two strings for equality


I am trying to compare two strings in Smalltalk, but I seem to be doing something wrong.

I keep getting this error:

Unhandled Exception: Non-boolean receiver. Proceed for truth.

stringOne := 'hello'.
stringTwo := 'hello'.
myNumber := 10.

[stringOne = stringTwo ] ifTrue:[
   myNumber := 20].

Any idea what I'm doing wrong?


Solution

  • Try

    stringOne = stringTwo 
         ifTrue: [myNumber := 20]`
    

    I don't think you need square brackets in the first line

    Found great explanation. Whole thing is here

    In Smalltalk, booleans (ie, True or False) are objects: specifically, they're instantiations of the abstract base class Boolean, or rather of its two subclasses True and False. So every boolean has type True or False, and no actual member data. Bool has two virtual functions, ifTrue: and ifFalse:, which take as their argument a block of code. Both True and False override these functions; True's version of ifTrue: calls the code it's passed, and False's version does nothing (and vice-versa for ifFalse:). Here's an example:

    a < b
      ifTrue: [^'a is less than b']
      ifFalse: [^'a is greater than or equal to b']
    

    Those things in square brackets are essentially anonymous functions, by the way. Except they're objects, because everything is an object in Smalltalk. Now, what's happening there is that we call a's "<" method, with argument b; this returns a boolean. We call its ifTrue: and ifFalse: methods, passing as arguments the code we want executed in either case. The effect is the same as that of the Ruby code

    if a < b then
      puts "a is less than b"
    else
      puts "a is greater than or equal to b"
    end