-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex26.rb
98 lines (77 loc) · 2.38 KB
/
ex26.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
module Ex26
# This function will break up words for us.
def self.break_words(stuff)
words = stuff.split(' ')
words
end
# Sorts the words.
def self.sort_words(words)
words.sort()
end
# Prints the first word after popping it off.
def self.puts_first_word(words)
word = words.shift()
puts word
end
# Prints the last word after popping it off.
def self.puts_last_word(words)
word = words.pop()
puts word
end
# Takes in a full sentence and returns the sorted words.
def self.sort_sentence(sentence)
words = break_words(sentence)
sort_words(words)
end
# Puts the first and last words of the sentence.
def self.puts_first_and_last(sentence)
words = break_words(sentence)
puts_first_word(words)
puts_last_word(words)
end
# Sorts the words then prints the first and last one.
def self.puts_first_and_last_sorted(sentence)
words = sort_sentence(sentence)
puts_first_word(words)
puts_last_word(words)
end
end
puts "Let's practice everything."
puts "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs."
poem = <<POEM
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
POEM
puts "--------------"
puts poem
puts "--------------"
five = 10 - 2 + 3 - 6
puts "This should be five: %s" % five
def secret_formula(started)
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
puts "With a starting point of: %d" % start_point
puts "We'd have %d jeans, %d jars, and %d crates." % [beans, jars, crates]
start_point = start_point / 10
puts "We can also do that this way:"
puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)
sentence = "All good\tthings come to those who wait."
words = Ex26.break_words(sentence)
sorted_words = Ex26.sort_words(words)
Ex26.puts_first_word(words)
Ex26.puts_last_word(words)
Ex26.puts_first_word(sorted_words)
Ex26.puts_last_word(sorted_words)
sorted_words = Ex26.sort_sentence(sentence)
print sorted_words
Ex26.puts_first_and_last(sentence)
Ex26.puts_first_and_last_sorted(sentence)