Rails 3 - Remove items belonging to array B from array A
a = ["1","2","3","4","5","6"]
b = ["1","3"]
c = a - b
same as
c = a.reject{ |e| b.include? e }
Remove array elements that are present in another array
You can use .reject
to exclude all banned words that are present in the redacted
array:
words.reject {|w| redacted.include? w}
Demo
If you want to get the list of banned words that are present in the words
array, you can use .select
:
words.select {|w| redacted.include? w}
Demo
How can I remove an item in a Ruby array based on its content?
A Hash is used to store pairs of items. It is faster, and guarantees names will be unique.
$vars = {}
def create(name, value)
$vars[name] = value
end
Then deleting is trivial.
$vars.delete(name)
As is finding the value.
value = $vars[name]
Hashes remember the order in which keys were added. $vars.keys
will return an Array of the names in the order they were added. Then you can use find_index
to get where it appears in the list of keys.
index = $vars.keys.find_index(name)
Aside from letting the user know the order in which variables are declared, this isn't of much use. Variables don't have an order.
If you wanted to do this with arrays, first we'd fix create
. It's storing both the key and the value in the same Array.
create(a, 23)
create(b, 42)
# $var = ['a', 23, 'b', 42]
Instead, store each pair in its own array.
def create(name, value)
$vars << [name, value]
end
create(a, 23)
create(b, 42)
# $var = [['a', 23], ['b', 42]]
Then to delete, search just the first elements of each pair using index
. Then delete that index.
def delete(name)
idx = $vars.map(&:first).index(name)
$vars.delete_at(idx) if idx
return idx
end
Finding the value of a name would work similarly. Find the index of the matching name, look up that pair, return its second element (the value).
def find(name)
idx = $vars.map(&:first).index(name)
pair = $vars[idx]
return pair[1]
end
But don't do this, use a Hash.
Is it safe to delete from an Array inside each?
You should not rely on unauthorized answers too much. The answer you cited is wrong, as is pointed out by Kevin's comment to it.
It is safe (from the beginning of Ruby) to delete elements from an Array while each
in the sense that Ruby will not raise an error for doing that, and will give a decisive (i.e., not random) result.
However, you need to be careful because when you delete an element, the elements following it will be shifted, hence the element that was supposed to be iterated next would be moved to the position of the deleted element, which has been iterated over already, and will be skipped.
Delete array elements from another array
Just do as below using Array Difference
:-
a = [1,2,3,4,5]
b = [4,5,6,7,8]
a = a - b
a # => [1,2,3]
But I would suggest, don't modify the source array. Rather do
new_ary = a - b
How can I delete one element from an array by value
I think I've figured it out:
a = [3, 2, 4, 6, 3, 8]
a.delete(3)
#=> 3
a
#=> [2, 4, 6, 8]
Remove elements from an array into another array (Ruby)
a = [1, 2, 3, 4, 5, 6]
b = a.select { |i| i < 5 } # [1, 2, 3, 4]
a = a - b # [5, 6]
Delete array element if index position is greater than a specific value
Here are some other ways to return a
sans elements at indices >= index
, which is probably better expressed as "returning the first index
elements". All below return ["a", "b"]
).
a = ["a", "b", "c", "d", "e"]
index = 2
Non-destructive (i.e., a
is not altered)
a[0,index]
index.times.map { |i| a[i] }
Destructive (a
is modified or "mutated")
a.object_id #=> 70109376954280
a = a[0,index]
a.object_id #=> 70109377839640
a.object_id #=> 70109377699700
a.replace(a.first(index))
a.object_id #=> 70109377699700
Removing elements from array Ruby
You may do:
a= [1,1,1,2,2,3]
delete_list = [1,3]
delete_list.each do |del|
a.delete_at(a.index(del))
end
result : [1, 1, 2, 2]
Related Topics
I Want to Return a Single Result Using .Find() with Ruby on Rails
Ruby/Rails - Active Record Db Migration to MySQL - Timestamp Type
Adding Nested Attributes to Devise User Model
Understanding Usage of Symbols in Routes.Rb Files
Rails 3 Translations Within Models in Production
Browser-Based Uploads Using Post
How to Share Image and Description Using Social_Share_Button in Rails
Escaping Parameters in Set_Form_Data Post
Ruby Instance Method & Conditional Local Variable Assignment with Same Name
Render Erb from Database into View Problem Please Help!
Error When Pushing to Heroku - ...Appear in Group - Ruby on Rails
Proc Throws Error When Used with Do End
Activerecord::Recordnotfound in Articlescontroller#Show Couldn't Find Article Without an Id