More testing

Okay this one, was a tough one:

 

module Ex25

# This function will break up words for me.
def Ex25.break_words(stuff)
words = stuff.split(‘ ‘)
return words
 end

#sorts the words
def Ex25.sort_words(words)
return words.sort
end

 #Prints the first word after shifting it off
  def Ex25.print_first_word(words)
word = words.shift
puts word
end

#Prints the last word after popping it off
def Ex25.print_last_word(words)
word = words.pop
 puts word
end

#Takes in a full sentence and returns the sorted words
def Ex25.sort_sentence(sentence)
words = Ex25.break_words(sentence)
return Ex25.sort_words(words)
end

#Prints the first and last words of the sentence
def Ex25.print_first_and_last(sentence)
words = Ex25.break_words(sentence)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
end

#sorts the words then prints the first and last one.
def Ex25.print_first_and_last_sorted(sentence)
words = Ex25.sort_sentence(sentence)
Ex25.print_first_word(words)
Ex25.print_last_word(words)

end

end

 

To run the script i had to go to “irb” and import it from there with:

require “./ex25.rb”

The result looked like that:

irb(main):001:0> require “./ex25.rb”
=> true
irb(main):002:0> sentence = “All good things come to those who wait.”
=> “All good things come to those who wait.”
irb(main):003:0> words = Ex25.break_words(sentence)
=> [“All”, “good”, “things”, “come”, “to”, “those”, “who”, “wait.”]
irb(main):004:0> sorted_words = Ex25.sort_words(words)
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):005:0> Ex25.print_first_word(words)
All
=> nil
irb(main):006:0> Ex25.print_last_word(words)
wait.
=> nil
irb(main):007:0> words
=> [“good”, “things”, “come”, “to”, “those”, “who”]
irb(main):008:0> Ex25.print_first_word(sorted_words)
All
=> nil
irb(main):009:0> Ex25.print_last_word(sorted_words)
who
=> nil
irb(main):010:0> sorted_words = Ex25.sort_sentence(sentence)
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):011:0> sorted_words
=> [“All”, “come”, “good”, “things”, “those”, “to”, “wait.”, “who”]
irb(main):012:0> Ex25.print_first_and_last(sentence)
All
wait.
=> nil
irb(main):013:0> Ex25.print_first_and_last_sorted(sentence)
All
who
=> nil

 

First of all I had to remember what “return” was for:

if a function runs return, I’m able to get the result of what comes after return, like

return words.sort

and use it for another function for example.

 

If I only put it like

def Ex25.print_first_word(words)

word = words.shift

puts word

end

it just prints it out on the screen and it doesnt get saved for later usage.

 

To explain the code and what function runs and when:

  1. #the first function runs and splits the words by SPACEBAR and saves the result for the next function, then…
  2. #the second function runs with the words split up. The words get sort by alphabetical order and also gets saved for…
  3. #the third function that just prints the first word of the ordered and split up sentence
  4. #the fourth function prints the last word.
  5. #the fifth function combines the first and the second, it splits the sentence up and sorts them also, and assigns them to sorted_words
  6. #the sixth function combines spliting words, first_word and last_word functions and prints the first and last word out
  7. #the last function basically sorts the words first, then puts the first and last words out.

 

——————————Halfway Done , Yay!—————————-

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s