assert
assert true
can pass the test
assert_equal false, false
Both are equal to pass the test
true or false
Everything will be treated as true except false and nil
Obj.is_a?(Obj)
can be used to determine if the query is of the type
strings
conversion
# They are all equal
"He said, \"Don't\""
'He said, "Don\'t"'
%{He said, "Don't"}
%(He said, "Don't")
%!He said, "Don't"!
multiple lines
\n
will be treated as a character and a single line
<<EOS
and EOS
host bank is treated as non-existent
long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS
append content
+= and <<
There are two methods that can be used to append content, but it's recommmended to use <<
, because <<
direct modification of the original object, but +=
will create a new object which will slow down the performance
"Hi" += "There"
"Hi" << "There"
confuse
"\n"
will be treated as 1 character
'\n'
will be treated as 2 character
interpolated value
value = 1
puts "The value is #{value}"
substring
str[num1,num2]
intercept num2 characters starting from num1
str[num1..num2]
intercept characters from num1 to num2
str[num]
intercept num character
The first character number is 0
string = "Bacon, lettuce and tomato"
assert_equal "let", string[7,3]
assert_equal "let", string[7..9]
assert_equal "a", string[1]
single character
single characters are represented by strings after ruby 1.8
assert_equal "a", ?a
assert_equal false, ?a == 97
split
.split
default split by spaces, but support patterns which are formed from regular expressions.
assert_equal ["Sausage", "Egg", "Cheese"], "Sausage Egg Cheese".split
assert_equal ["the", "rain", "in", "spain"], "the:rain:in:spain".split(/:/)
join
words = ["Now", "is", "the", "time"]
assert_equal "Now is the time", words.join(" ")
标签:will,01,string,character,equal,assert,He
From: https://www.cnblogs.com/CodeAndMoe/p/17034972.html