And The include method returns true if a value if found. If a value is not found, it returns false.
Next The program searches the "values" array for 999, which is not found. It prints a helpful message.
values = [10, 200, 3000]
# See if array includes this value.
puts "ARRAY CONTAINS 200"
# See if array does not include this value.
puts "ARRAY DOES NOT CONTAIN 999"
endARRAY CONTAINS 200
ARRAY DOES NOT CONTAIN 999
String include. Is one string contained within another? The "include?" method in Ruby tells us. It searches one string for a second string. It returns true or false.
Here We see that the string "plato" contains the string "to". It does not contain the string "not".
And The "include?" method is case-sensitive. This means the string "plato" does not include the string "PLA".
value = "plato"# String includes "to".
if value.include? "to"
# String does not include "not".
if !value.include? "not"
# String does not include "PLA" uppercase.
if !value.include? "PLA"
Some notes. In Ruby, a method that ends in a question mark is a predicate method, one that returns true or false. With "include we get a boolean result.
A summary. Methods that use iterators usually will involve more code than "include." But they also may be more powerful—you can add more steps to the iterator than you can to "include."
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 5, 2021 (edit link).