Skip to content

Commit dbc0dab

Browse files
authored
Merge pull request #24 from csalmeida/challenge/ruby-blanks
Challenge: Ruby Blanks
2 parents 6b55344 + d7c443c commit dbc0dab

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
3232
- [Scripting](#scripting)
3333
- [Best Practices](#best-practices)
3434
- [Exit a running script](#exit-a-running-script)
35+
- [Input and output](#input-and-output)
3536
- [Challenge: Guessing Game](challenges/guessing-game.rb)
3637
- [Enumerables and Code Blocks](#enumerables-and-code-blocks)
3738
- [Enumerables](#enumerables)
@@ -121,7 +122,7 @@ Using `ri` allows documentation to be accessed offline if needed.
121122
## Challenges
122123
There are a few challenges that were completed to solidify knowledge for each chapter. Consult [`/challenges`](challenges/) to access them, both include a file with the challenge and another with a `solution.rb`.
123124

124-
> Challenges were designed by [Kevin Skoglund](https://twitter.com/kskoglund) for the [Ruby Essential Training course](https://www.lynda.com/Ruby-tutorials/Challenge-Roman-numerals/769286/807388-4.html) (linda.com)
125+
> Challenges were designed by [Kevin Skoglund](https://twitter.com/kskoglund) for the [Ruby Essential Training: 1 The Basics](https://www.linkedin.com/learning/ruby-essential-training-1-the-basics/) (linkedin.com)
125126
126127
# Object Types
127128
An introduction to the various object types (also referred as data types) available in Ruby and how they work.
@@ -1449,4 +1450,6 @@ puts hash_1.merge(hash_2) {|key,old,new| old * new } # {:a => 6, :b => 16, :c =>
14491450

14501451
Since `:b` also presents a `key` conflict even if they have the same values, the block logic also applies to it.
14511452

1452-
The merge method also has a `merge!` version which replaces the contents of a hash.
1453+
The merge method also has a `merge!` version which replaces the contents of a hash.
1454+
1455+
> There's a challenge available for this chapter: [Ruby Blanks](challenges/ruby-blanks.rb)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!usr/bin/env ruby
2+
3+
game_title = 'Ruby Blanks Game'
4+
5+
# Wraps the game title with dashes.
6+
game_title = "| #{game_title} |"
7+
game_title.length.times do
8+
print '-'
9+
end
10+
11+
puts "\n#{game_title}"
12+
13+
game_title.length.times do
14+
print '-'
15+
end
16+
puts
17+
18+
# Sets a value to look for blanks in sentences.
19+
BLANK_FLAG = '-----'
20+
# Stores all sentences and types of blanks in a single place.
21+
SENTENCES = [
22+
{
23+
:text => "I went to visit a #{BLANK_FLAG} and found a #{BLANK_FLAG}, it looked #{BLANK_FLAG} so I started to #{BLANK_FLAG}.",
24+
:blanks => ['noun', 'noun', 'adjective', 'verb']
25+
},
26+
{
27+
:text => "Next thing you know, the #{BLANK_FLAG} was having a #{BLANK_FLAG} like everyone else. It was #{BLANK_FLAG} and #{BLANK_FLAG}.",
28+
:blanks => ['noun', 'verb', 'adjective', 'adjective']
29+
}
30+
]
31+
32+
# Selects a random sentence from the available ones.
33+
random_position = rand(0..(SENTENCES.length - 1))
34+
selected_sentence = SENTENCES[random_position]
35+
36+
# Prompts user to fill in the blanks, one by one.
37+
# Assigns answers to a responses array.
38+
# The array will have the same items as blanks as a map is being executed.
39+
responses = selected_sentence[:blanks].map do |blank|
40+
indefinite_article = blank.start_with?('a') ? 'an' : 'a'
41+
print "Give me #{indefinite_article} #{blank}: "
42+
response = gets.chomp
43+
end
44+
45+
# Converts sentence into an array enumerable to become easier to iterate through.
46+
selected_sentence[:text] = selected_sentence[:text].split(' ')
47+
48+
# This index is used to assign responses to the blanks.
49+
index = -1
50+
# Creates a sentence with the responses by checking if word has a a blank to be filled.
51+
# It replaces only the blank with the response in order to keep punctuation, if existent.
52+
complete_sentence = selected_sentence[:text].map do |word|
53+
if word.include?(BLANK_FLAG)
54+
index += 1
55+
word.sub!(BLANK_FLAG, responses[index])
56+
else
57+
word
58+
end
59+
end
60+
61+
# Turns sentence into a string to be displayed to the user.
62+
puts complete_sentence.join(' ')

challenges/ruby-blanks-solution.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!usr/bin/env ruby
2+
3+
# Title of the game
4+
puts '-' * 19
5+
puts '| Ruby Blanks Game|'
6+
puts '-' * 19
7+
8+
# Sets a value to look for blanks in sentences.
9+
blanks = ['noun', 'noun', 'adjective', 'verb']
10+
11+
# Prompts user to fill in the blanks, one by one.
12+
answers = blanks.map do |blank|
13+
indefinite_article = blank.start_with?('a') ? 'an' : 'a'
14+
print "Give me #{indefinite_article} #{blank}: "
15+
response = gets.chomp
16+
end
17+
18+
# Fills the blanks in the sentence with captured answers.
19+
text = "I went to visit a #{answers[0]} and found a #{answers[1]}, it looked #{answers[2]} so I started to #{answers[3]}."
20+
21+
# Shows complete sentence to the user.
22+
puts text

challenges/ruby-blanks.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!usr/bin/env ruby
2+
# Challenge: Ruby Blanks
3+
# Create a fill-in-the-blank game (known as Mad Libs).
4+
# Think of one or more sentences to with blanks to be completed.
5+
# For instance: "I decided to _____ a _____ party for my _____."
6+
# The sentence is not revealed before the blanks are filled in.
7+
# The blanks could be a noun, a verb or an adjective, add these types to an array.
8+
# Ask the user to give you words for each one of those.
9+
# For instance: "Give me a verb: <user types here>"
10+
# "Give me a adjective: <user types here>"
11+
# Output the whole sentence with the words the user chose.

0 commit comments

Comments
 (0)