ruby - How to remove elements from an array and keep them in a variable in order? -



ruby - How to remove elements from an array and keep them in a variable in order? -

i have array : input2 = ["other", "y", "x", "z", "description"] i want take off "y", "x", "z", "description" , store them in variable maintain them in order.example : input2 = ["z", "x", "y", "other", "description"] should have : input3 = ["other"] some_variable = ["z", "x", "y", "description"]

thanks help.

def take_it_off(arr, values) without = [] ordered_values = [] arr.each |val| if values.include? val ordered_values << val else without << val end end homecoming without, ordered_values end

so can do

irb> values = "y", "x", "z", "description" => ["y", "x", "z", "description"] irb> arr = ["z", "x", "y", "other", "description"] => ["z", "x", "y", "other", "description"] irb> take_it_off(arr, values) => [["other"], ["z", "x", "y", "description"]]

ruby-on-rails ruby

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -