1

Newbie here. Thanks in advance.

The idea here is akin to pulling a card at random from a deck of cards. I want to permanently pull a random number out of the array.

My code is giving me an error regarding the 'delete!' method. "Undefined method delete!' for [1, 2, 3, 4, 5, 6, 7, 8]:Array (repl):6:ininitialize'". But for all I know there might be half a dozen errors in here.

I've commented the code below so you can follow my amateur thought process. I'm sure there's some awesome way to write this all in two lines of code that I just haven't experienced yet. Can you help?

array = [1, 2, 3, 4, 5, 6, 7, 8]
# Create array of sequential numbers

high_number = array.length - 1
# Determining length of array for next line, offset by 1 for array

rand_number = rand(0..high_number)
# Create a random number from 0 (first array position) through high num

draw = array[rand_number]
# drawing that position number from array (passed on to another action)

array.delete!(rand_number)
# make sure the next time I loop through this array that number can't be drawn again

4 Answers 4

2

There is no delete! method for an array. Perhaps you meant delete?

delete(obj) → item or nil

Deletes all items from self that are equal to obj.

Returns the last deleted item, or nil if no matching item is found.

If the optional code block is given, the result of the block is returned if the item is not found. (To remove nil elements and get an informative return value, use #compact!)

a = [ "a", "b", "b", "b", "c" ]
a.delete("b")                   #=> "b"
a                               #=> ["a", "c"]
a.delete("z")                   #=> nil
a.delete("z") { "not found" }   #=> "not found"

Or, judging by the way you're using it, perhaps you really meant delete_at:

delete_at(index) → obj or nil

Deletes the element at the specified index, returning that element, or nil if the index is out of range.

See also #slice!

a = ["ant", "bat", "cat", "dog"]
a.delete_at(2)    #=> "cat"
a                 #=> ["ant", "bat", "dog"]
a.delete_at(99)   #=> nil

You may also be interested in looking at some of the other methods on Array, such as shuffle, sample, and pop. Complete documentation is availble on ruby-doc.org.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This gets me to a new error, but one problem down. :)
0

If you are deleting the item just to make sure it's not drawn again then Array#sample is a possibility

ar = (1..8).to_a
p ar.sample(3) # => [1, 8, 6]

The array remains the same.

Comments

0

As Ajedi32 says, look into shuffle and pop and get to know the various array methods.

Then you can rewrite your code as:

array = (1..8).to_a
# Create array of sequential numbers

draw = array.shuffle!.pop
# randomize order of array in place, remove and return last element

Comments

0
>> a = (1..8).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8]
>> b = a.delete(a.sample)
=> 6
>> a
=> [1, 2, 3, 4, 5, 7, 8]
>> b
=> 6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.