The compare()
method compares two items and returns values indicating less than, equal to, or greater than as a result of the comparison.
compare(value_to_compare, reference_value)
value_to_compare
: This is a value of an object data type that we want to compare to another value.reference_value
: This is the value of an object data type that serves as a reference point for the comparison.This method returns an atom, which can be -1
, 1
or 0
.
0
: If compared objects are identical.-1
:If value_to_compare
is less than reference_value
.1
: If value_to_compare
is greater than reference_value
.print(1,compare("group","group"))puts(1,'\n')print(1,compare("GrouP","group"))puts(1,'\n')print(1,compare("groups","group"))puts(1,'\n')print(1,compare("group","grouping"))
The puts()
method in the code snippet is used to print a new line so that each code will execute on a line new. For each of the comparisons, the first parameter of the compare function is the value that will be compared with the second value which serves as the reference value.
0
because the compared values are identical.-1
, which indicates that the compared value is less than the reference value. The same is the case of code in line 12.1
.