How would I go about writing a ‘Mad Libs’ type program using Ruby?
Any examples would be greatly appreciated.
Thanks.
How would I go about writing a ‘Mad Libs’ type program using Ruby?
Any examples would be greatly appreciated.
Thanks.
On Oct 3, 2009, at 9:39 AM, Britt Hayes wrote:
How would I go about writing a ‘Mad Libs’ type program using Ruby?
Any examples would be greatly appreciated.
Have a look at the solutions to this old Ruby Q.:
James Edward G. II
I had looked at those before, but I couldn’t follow very well as I am
very new to Ruby.
Would you be able to get me started using one of those examples?
How does the mad lib script I wrote work with the commands?
On Oct 3, 2009, at 2:19 PM, John K. wrote:
I had looked at those before, but I couldn’t follow very well as I am
very new to Ruby.Would you be able to get me started using one of those examples?
How does the mad lib script I wrote work with the commands?
Here’s an example of me using the code from the first solution to play
with the example in the quiz:
$ ruby madlib.rb madlib.txt
Give me a gemstone:
Ruby
Give me a gemstone:
Emerald
madlib
Our favorite language is Ruby. We think Ruby is better
than Emerald.
$ cat madlib.rb
def ask_for(str)
puts “Give me #{str}:”
$stdin.gets.chomp
end
keys={}
puts “”, ARGV[0].split(".")[0].gsub("_", " "),
IO.read(ARGV[0]).gsub(/((([^)]+)))/) {
if (t=$1) =~ /\A([^:]+):(.+)\z/
keys[$1]=ask_for($2)
else
keys[t] || ask_for(t)
end
}
$ cat madlib.txt
Our favorite language is ((gem:a gemstone)). We think ((gem)) is better
than ((a gemstone)).
Hope that helps.
James Edward G. II
On Oct 3, 2009, at 3:47 PM, John K. wrote:
$ ruby madlib.rb madlib.txt
I am using nano for a script shell. Does that have an effect on this?
I believe you mean that you are using nano for your text editor, not
shell. Most likely your shell is bash, if you are on a Unix and
haven’t tried to change it. The commands I showed, lines starting
with $, will work fine in bash.
Give me a gemstone:
Ruby
Give me a gemstone:
EmeraldWhere does this get written?
This we me interacting with the program as it ran. It asked those
questions and I typed those answers.
madlib
Our favorite language is Ruby. We think Ruby is better
than Emerald.So, I guess my biggest question is where does the script get written
before you start writing the program to fill in the gaps?
Using nano, I can create the file like this:
Does that help?
James Edward G. II
$ ruby madlib.rb madlib.txt
I am using nano for a script shell. Does that have an effect on this?
Give me a gemstone:
Ruby
Give me a gemstone:
Emerald
Where does this get written?
madlib
Our favorite language is Ruby. We think Ruby is better
than Emerald.
So, I guess my biggest question is where does the script get written
before you start writing the program to fill in the gaps?
$ cat madlib.rb
def ask_for(str)
puts “Give me #{str}:”
$stdin.gets.chomp
endkeys={}
puts “”, ARGV[0].split(".")[0].gsub("_", " "),
IO.read(ARGV[0]).gsub(/((([^)]+)))/) {
if (t=$1) =~ /\A([^:]+):(.+)\z/
keys[$1]=ask_for($2)
else
keys[t] || ask_for(t)
end
}
$ cat madlib.txt
Our favorite language is ((gem:a gemstone)). We think ((gem)) is better
than ((a gemstone)).Hope that helps.
James Edward G. II
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs