Array position


#1

Arrays have the .include? function which tells you if an array has an
element.

is there a .position? function to get the location of that element? I
can loop through no problem, but was just wondering if there was
anything built in.


#2

Hi Alex,

Alex T. wrote:

Arrays have the .include? function which tells
you if an array has an element.

is there a .position? function to get the location
of that element?

index (and it’s a method (aka action), not a function)

From the Ruby Library Reference section of Pickaxe (aka “Programming
Ruby”),
p434, in the Array section, right under the include? method: “Returns
the
index of the first object in arr that is == to obj. Returns nil if no
match is found.”

a=[“a”,“b”,“c”]
a.index(“b”) --> 1

Pick up the Pickaxe. It’s indispensible if you’re going to work in Ruby
on
Rails.

Best regards,
Bill


#3

Thanks for the clear explanation Bill. I was looking online for info,
but the sites I checked never mentioned that function.

As soon as I get through my current book, Pickaxe will be next on the
list.

Alex


#4

Hi Alex,

Thanks for the clear explanation Bill.

You’re welcome. Glad it was helpful.

I was looking online for info, but the sites I checked
never mentioned that function.

As RoR’s ease-of-use attracts more who are new to web application
development), the sophistication of the current Ruby and Rails online
documentation increasingly comes up as ‘problem area’. My take on it
is:
It is what it is, not what it will become. Timing is a personal choice.
Having said that, there are, IME, four key resources, the use of which
will
enable your success at this point: Google, The Books, this list, and
tenacity.

As soon as I get through my current book, Pickaxe will
be next on the list.

Pickaxe isn’t like that. In addition to the Ruby tutorial, it’s the
definitive English-language Ruby language reference. I’ve learned tons
just
cruising through the methods in the Class modules. And I think that’s
really helped my intuition about how to approach new stuff. I recommend
ordering it today.

Best regards,
Bill