TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to RUBY

Ruby include: Array Contains Method

Determine if an array contains a value by searching it with the include method.
Include, array. Does an array contain a specific value? With Ruby we can use a loop or an iterator to search an array. But another approach can be used.While, UntilIterator
With include, we can search an array for a value in a single method call. This is easier to read and easy to use. We must include a question mark at the end of include.
An example. Let us begin. This program uses an array of 3 integers. It then searches the array with "include?" 2 times. The first tested value, 200, is found.Array

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.

Ruby program that uses include method on array values = [10, 200, 3000] # See if array includes this value. if values.include?(200) puts "ARRAY CONTAINS 200" end # See if array does not include this value. if !values.include?(999) puts "ARRAY DOES NOT CONTAIN 999" end Output ARRAY CONTAINS 200 ARRAY DOES NOT CONTAIN 999
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."
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf