她是我的 ruby 脚本:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
puts books.sort! {
|firstBook, secondBook|
firstBook <=> secondBook
}
我原以为它会打印 -1,0 或 1,但它打印了书名。如何在控制台中获取它们的 boolean 值。
类似地,在 Java 中我的代码是:
public class sampletest {
public static void main(String[] args) {
String String1 = "hello";
String String2 = "Hello";
System.out.println(String1.equals(String2));
}
}
这里的输出是:True( boolean 值) 我是 Ruby 新手,我不明白这两者之间的区别
最佳答案
在第一个示例中,您进行排序(涉及大量您看不到的比较)。在第二个示例中,您只需进行比较。 Java 代码片段的 Ruby 等效项是:
string1 = "hello"
string2 = "Hello"
puts(string1 == string2)
关于java - Ruby 的比较器 <=> Vs. Java 的 .equals;比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26988734/