RUBY: How can I print only the words that start with a certain letter from an Array?

Thallius

-------So what I have so far is-----------

array = ["milk", "bread", "pizza", "eggs", "soda", "beer"]

def first_letter(x)
  if x.start_with?(b)
     puts x
  end
end

first_letter(array)

Really confused on what I need to do. I've checked Ruby Docs and I still can't figure out how to single out just the words that start with "B" in my array and print them to the screen. Any help would be much appreciated!

Andrey Deineko

What you want is basically select the elements, matching the condition.

To this end you can use Array#select:

array.select { |word| word.start_with?('b') }
#=> ["bread", "beer"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# how do you print words that start with a specific letter from a .txt file

From Dev

How can I print only certain commands from a bash script as they are run?

From Dev

How can I print only certain commands from a bash script as they are run?

From Dev

How can I print my array in a certain way?

From Dev

How to remove words from text file in Python that contain certain letter?

From Dev

How can I only keep items of an array that match a certain condition?

From Dev

How to extract letter words only from an arbitrary input file

From Dev

How can I capitalize a letter from a word one at a time, then add each instance of the word with a caps letter into a array?

From Java

How can I print a result from a nested for loop only once

From Dev

How can I print a result from a nested for loop only once

From Dev

How do I remove certain words from a string, but only if they appear at the end of the string?

From Dev

Print words starts from a particular letter in python?

From Java

How to extract the first letter of certain words

From Dev

How to Split words and separate the ones with a certain letter?

From Java

How can I ignore certain returned values from array destructuring?

From Dev

How can I loop an array from certain position in clockwise and counterclockwise?

From Dev

Only allow HTML field to start with certain words

From Dev

Match words that don't start with a certain letter using regex

From Dev

How can I search directories starting with a certain letter?

From Dev

How to I parse a file and only keep certain words?

From Dev

How can I print text with spaces from array?

From Dev

How can I print multiple elements from an array inJavaSCript?

From Dev

How can I print element of array from Flask on javascript on HTML

From Dev

Can I configure tomcat only to start certain webapps on startup?

From Dev

Can I configure tomcat only to start certain webapps on startup?

From Dev

how do i cascade windows in my mdiparent but only to start it from a certain location

From Dev

How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

From Dev

Print Char Array in C - only last letter?

From Dev

How can I create instances of a ruby class from a hash array?

Related Related

  1. 1

    C# how do you print words that start with a specific letter from a .txt file

  2. 2

    How can I print only certain commands from a bash script as they are run?

  3. 3

    How can I print only certain commands from a bash script as they are run?

  4. 4

    How can I print my array in a certain way?

  5. 5

    How to remove words from text file in Python that contain certain letter?

  6. 6

    How can I only keep items of an array that match a certain condition?

  7. 7

    How to extract letter words only from an arbitrary input file

  8. 8

    How can I capitalize a letter from a word one at a time, then add each instance of the word with a caps letter into a array?

  9. 9

    How can I print a result from a nested for loop only once

  10. 10

    How can I print a result from a nested for loop only once

  11. 11

    How do I remove certain words from a string, but only if they appear at the end of the string?

  12. 12

    Print words starts from a particular letter in python?

  13. 13

    How to extract the first letter of certain words

  14. 14

    How to Split words and separate the ones with a certain letter?

  15. 15

    How can I ignore certain returned values from array destructuring?

  16. 16

    How can I loop an array from certain position in clockwise and counterclockwise?

  17. 17

    Only allow HTML field to start with certain words

  18. 18

    Match words that don't start with a certain letter using regex

  19. 19

    How can I search directories starting with a certain letter?

  20. 20

    How to I parse a file and only keep certain words?

  21. 21

    How can I print text with spaces from array?

  22. 22

    How can I print multiple elements from an array inJavaSCript?

  23. 23

    How can I print element of array from Flask on javascript on HTML

  24. 24

    Can I configure tomcat only to start certain webapps on startup?

  25. 25

    Can I configure tomcat only to start certain webapps on startup?

  26. 26

    how do i cascade windows in my mdiparent but only to start it from a certain location

  27. 27

    How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

  28. 28

    Print Char Array in C - only last letter?

  29. 29

    How can I create instances of a ruby class from a hash array?

HotTag

Archive