Just wondering if there is the same method for an Array object that is similar to indexOf in JavaScript?
For instance:
arr =% w {'a', 'b', 'c'} c = 'c' if (arr.indexOf (c)! = -1) // do some stuff else // don't do some stuff
This is the .index array method.
.index
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-index
In ruby, only false and nil are considered false, so you can simply do:
false
nil
arr = %w{a, b, c} c = 'c' if arr.index c # do something else # do something else end
If you want to check for an element in an array, can you use include? :
include?
if arr.include?(c) # do stuff else # don't end
Use the Array # parameter for this:
c = 'c' %w{abc}.index(c)
if arr.last == c # do some stuff else # don't do some stuff end
Source: https://habr.com/ru/post/1436540/More articles:Audio playback and spectrum analysis libarary for C # - c #Java: How to check jdk version? - javaAndroid - how do you manage multiple layout files according to DRY principle? - androidmySQL SUM and COUNT, some values ββdouble when merged - sqlGet function type from scala console - scalaRuby. Why arr =% w {'a', 'b', 'c'} => ["'a',", "'b',", "'c'"] - arraysMake button transparent over mouse - windows-8https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1436543/how-to-reduce-size-of-moov-atom-of-h264-movies-to-improve-streaming-start-for-smartphones&usg=ALkJrhia-qxN9KfliAjiPpKPCw33sE-zyAHow to load the corresponding Default.png in UImageView? - iosHow to interpret g ++ generated .i file - c ++All Articles