Creating a method to modify elements in an array in Ruby with 2 arguments, one of them being the original array -
Creating a method to modify elements in an array in Ruby with 2 arguments, one of them being the original array -
i create method, mod_method(array, n)
, array
array , n
number. mod_method
should take number n
, add together internal numbers in array
, homecoming new array.
for example, using array = ["i", "have", 3, "to", 4, "hours"]
, how find mod_method(array, 1)
such
mod_method(array,1) => ["i", "have", 4, "to", 5, "hours"]
i'm noob , able using defined array , number (let's utilize 1), such:
array = ["i", "have", 3, "to", 4, "hours"] =>[[0] "i", [1] "have", [2] 3, [3] "to", [4] 4, [5] "hours"] numbers = array.values_at(2, 4) => [ [0] 3, [1] 4 mod = numbers.map{|x| x + 1} => [ [0] 4, [1] 5] new_array = ["i", "have", mod[0], "to", mod[1], "hours"] => ["i", "have", 4, "to", 5, "hours"]
i have no thought how undefined arguments mod_method
.
here do.
def mod_method(array,n) array.map |e| e.is_a?(integer) ? e + n : e end end
ruby arrays methods
Comments
Post a Comment