mariam klait
mariam klait

Reputation: 19

Iterate through an array and push elements into individual new arrays

I have an array:

["apple", "banana", "animal", "car", "angel"]

I want to push elements that start with "a" into separate arrays. I want to return:

["apple"], ["animal"], ["angel"]

I have only been able to make it work if I push them into an empty array that I pre-created.

Upvotes: 0

Views: 2726

Answers (5)

bkunzi01
bkunzi01

Reputation: 4561

array_of_arrays = []
your_array.each do |ele|
  if ele.starts_with?("a")
    array_of_arrays << ele.to_a
  end
end

Upvotes: 0

djaszczurowski
djaszczurowski

Reputation: 4515

Generally in order to pick elements from array that match some specific conditione use select method.

select returns an array of all elements that matched critera or an empty list in case neither element has matched

example:

new_array = array.select do |element|
  return_true_or_false_depending_on_element(element)
end

now when we would like to put every element in its own array we could you another array method that is available on array - map which takes every element of an array and transforms it in another element. In our case we will want to take every matching string and wrap it in array

map usage:

new_array = array.map do |element|
  element_transformation(element) # in order to wrap element just return new array with element like this: [element]
end

coming back to your question. in order to verify whether a string starts with a letter you could use start_with? method which is available for every string

glueing it all together:

strings = ["apple", "banana", "animal", "car", "angel"]

result = strings.select do |string|
  string.start_with?("a")
end.map do |string_that_start_with_a|
  [string_that_start_with_a]
end

puts result

Upvotes: 4

lobati
lobati

Reputation: 10215

Here's a golfed down version:

array.grep(/\Aa/).map(&method(:Array))

I might consider my audience before putting something this clever into production, since it can be a little confusing.

Array#grep returns all elements that match the passed regular expression, in this case /\Aa/ matches strings that begin with a. \A is a regular expression token that matches the beginning of the string. You could change it to /\Aa/i if you want it to be case insensitive.

The &method(:Array) bit grabs a reference to the kernel method Array() and runs it on each element of the array, wrapping each element in the array in its own array.

Upvotes: 1

Nick Ellis
Nick Ellis

Reputation: 1077

Here is some code I got to do this in console:

> arr = ["apple", "banana", "animal", "car", "angel"]
=> ["apple", "banana", "animal", "car", "angel"] 
> a_words = []
=> [] 
arr.each do |word|
  a_words << word if word.chars.first == 'a'
end
=> ["apple", "banana", "animal", "car", "angel"]
> a_words
=> ["apple", "animal", "angel"]

If you wanted to do something more complex than first letter you might want to use a regex like:

if word.matches(/\Aa/)  # \A is beginning of string

Upvotes: 0

toto
toto

Reputation: 455

The simplest I can come up with is:

arr = %w{ apple banana animal car angel }

arr.map {|i| i.start_with?('a') ? [i] : nil }.compact

=> [["apple"], ["animal"], ["angel"]]

Upvotes: 0

Related Questions