What's the difference between ..
and ...
range methods in Ruby?
The purpose of ordered_vowel_words
is to sort through an entire string, and then to select only words which have vowels that appear in order, where order is their appearance in the alphabet as well as in the word.
For example, "Determining" would be an ordered_vowel_word
because the e's appear before the i's in not only the word but in the alphabet as well:
def ordered_vowel_words(str)
words = str.split(" ")
ordered_vowel_words = words.select do |word|
ordered_vowel_word?(word)
end
ordered_vowel_words.join(" ")
end
This is where I'm having trouble understanding the indexing:
irb(main):029:0> first = (1..10)
=> 1..10
irb(main):030:0> p first
1..10
=> 1..10
I notice that using the ..
range first contains values 1-10
:
irb(main):031:0> second = (1...10)
=> 1...10
irb(main):032:0> p second
1...10
=> 1...10
However I also noticed that ...
for the range also returns the exact same values:
def ordered_vowel_word?(word)
vowels = ["a","e", "i", "o", "u"]
letters_array = word.split("")
vowels_array = letters_array.select { |l| vowels.include?(l) }
(0...(vowels_array.length - 1)).all? do |i|
vowels_array[i] <= vowels_array[i+1]
end
end
If I use ..
instead of ...
then I receive an error, which I find confusing.
If I use the ..
method then I iterate through the entire length of the array:
irb(main):033:0> first.each do |num|
irb(main):034:1* puts num
irb(main):035:1> end
1
2
3
4
5
6
7
8
9
10
=> 1..10
Whereas if I use the ...
method then I will stop one short of the entire array:
irb(main):036:0> second.each do |num|
irb(main):037:1* puts num
irb(main):038:1> end
1
2
3
4
5
6
7
8
9
=> 1...10
I'm confused why ...
is correct. I feel as if it will stop short of the full length of the array. I consulted http://ruby-doc.org/ but am continuing to have issues understanding this concept.
Sorry in advance if it's a small error I've made, like with all?
maybe?
I just read the the question which I found explaining the difference between ..
and ...
.
I know that ...
doesn't include the last index of the array, however, that leaves my question unanswered again. If the word were "aura" wouldn't the last "a" not be included when I use the ...
method, because the ...
method ignores the last index of the array?