0

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
PrimeTimeTran
  • 1,807
  • 2
  • 17
  • 29
  • 2
    Sigh... I just googled so more rearranging my search words and found this... http://stackoverflow.com/questions/9690801/difference-between-double-dot-and-triple-dot-in-range-generation . Please don't yell at me =(~! I'm reading it now... lol – PrimeTimeTran Jan 03 '16 at 16:33
  • EDIT: 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... please allow me to articulate. 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??? – PrimeTimeTran Jan 03 '16 at 16:41
  • 1..10 means all numbers from 1 to 10 (inclusive), whereas 1...10 means all numbers from 1 up till 9. So you can see the triple dot as all up to but excluding the last number. – Nabeel Jan 03 '16 at 16:49
  • @NabeelAmjad I totally get that. I'm just confused how I'm *not* getting an index error the way it is written right now... like I said in my second comment, if I were to use the string "aura" for example, if we don't include the last index then "a" wouldn't be iterated over, thus my method would be buggy.. correct? It would show that "aura" is an ordered vowel word, when it isn't. Or am I just being a novice and doing the indexing incorrectly..? – PrimeTimeTran Jan 03 '16 at 16:52
  • 1
    @mikej I'm totally with you on that, that'd make my life so much easier. However if I remove the "- 1" from vowels_array.length I receive an error of "`<=': comparison of String with nil failed (ArgumentError)". Maybe I don't understand the .all method well enough...? – PrimeTimeTran Jan 03 '16 at 16:56
  • @Primetime sorry, I'd misread your comparison at first.. you want to leave the code as it is now because your comparison `vowels_array[i] <= vowels_array[i+1]` is comparing a letter with the letter after it.. without the `-1` your last comparison would be going beyond the end of `vowel_array` – mikej Jan 03 '16 at 17:00
  • @Primetime so with the code as it is now, for `"aura"` the `vowels_array` would be `["a", "u", "a"]` and the range would be `(0...2)` i.e. 0 and 1 so the comparisons would be `"a" <= "u"` (true) and `"u" <= "a"` (false!) ... so false overall because of the use of `all?` – mikej Jan 03 '16 at 17:03
  • 1
    @mikej yes! that makes so much sense when you break it down like that. Simply put, I didn't understand `all?` well enough. Thank you for clarifying! I appreciate it a lot. – PrimeTimeTran Jan 03 '16 at 17:09
  • 1
    I guess I was so focused on making sure the iterations went through the array in it's entirety that I loss focus and forgot that as soon as soon as the `.all?` method failed, then `ordered_vowel_word?` would return false to `ordered_vowel_words` Thanks again @mikej – PrimeTimeTran Jan 03 '16 at 17:12
  • Cool stuff! Glad that was helpful – mikej Jan 03 '16 at 17:13

0 Answers0