End of sentience has ? y/n, if n please use one "string?"=o.k

Hello all

Still new to programing.Â
I’m making a magic 8 ball clone, for fun and to learn by, console based
for now. :)Â


I need a few good examples on how to detect if input has a question mark
at the end (more than one example/explanation preferably.)Â

So, I GETS a question in my .rb and if “string?” o.k. else I’d like to
say, need a question with a ? mark at end please.


At http://www.ruby-lang.org/en/
[
Ruby is…Â Â Â Â Â Â Â Â Â Â Â # Output “I love Ruby”
----------------Â Â Â Â Â Â Â Â Â say = “I love Ruby”
e pluribus          puts say
unum, and such
:)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â # Output “I LOVE RUBY”
                say[‘love’] = “love
                puts say.upcase
Â
               # Output “I love Ruby”
                # five times
                5.times { puts say }
]
I see I can brake string’s into parts, this may help me rite?

Ruby rocks, thanks to all.

On Thu, Mar 25, 2010 at 8:32 PM, jamison edmonds
[email protected] wrote:

Still new to programing.
I’m making a magic 8 ball clone, for fun and to learn by, console based for now. :slight_smile:

I need a few good examples on how to detect if input has a question mark at the end (more than one example/explanation preferably.)

So, I GETS a question in my .rb and if “string?” o.k. else I’d like to say, need a question with a ? mark at end please.

There are quite a few ways to approach this problem. If you haven’t
already, you can find an overview of various string methods here:
http://www.ruby-doc.org/core/classes/String.html

First let’s gets a string:

a = gets
abcdefg?
=> “abcdefg?\n”

I’m assuming you don’t care about the newline on the end of gets-ed
string. So, I’ll chomp it first:
http://ruby-doc.org/core/classes/String.html#M000819

a.chomp
=> “abcdefg?”
a
=> “abcdefg?\n”
a.chomp!
=> “abcdefg?”
a
=> “abcdefg?”

Strings in ruby can in some ways behave like arrays (responding to []):
http://ruby-doc.org/core/classes/String.html#M000771

substring from last (-1) position to last position (-1)

a[-1…-1]
=> “?”

substring of length 1 from last (-1) position

a[-1,1]
=> “?”

(There is also simply a method like a[-1]:

a[-1]
=> 63
which returns the code for position -1, if you look 63 up in an ASCII
table, you’d see that it corresponds to ?:
63.chr
=> “?”
but, this behavior is different from ruby 1.8 and ruby 1.9, which is
why I almost didn’t mention it here.)

Strings can also be turned into arrays:
http://ruby-doc.org/core/classes/String.html#M000803

b = a.split(‘’)
=> [“a”, “b”, “c”, “d”, “e”, “f”, “g”, “?”]
b.last
=> “?”

And strings can also be matched against patterns:
http://ruby-doc.org/core/classes/String.html#M000778

does a have a ! at the end

a.match(/!$/)
=> nil

does a have a ? at the end

a.match(/?$/)
=> #<MatchData “?”>

On Fri, Mar 26, 2010 at 9:32 AM, jamison edmonds
[email protected] wrote:

               # Output “I love Ruby”
                # five times
                5.times { puts say }
]
I see I can brake string’s into parts, this may help me rite?

Ruby rocks, thanks to all.

require ‘readline’

puts “You will be talking to the awesome 8ball, type Control-d to
finish your conversation.”

while line = Readline.readline(‘> ‘, true)
if line.end_with?(’?’)
puts “Consider it carefully.”
else
puts “How about you ask some question?”
end
end

Thanks to all :slight_smile:
Â

— On Thu, 3/25/10, [email protected] [email protected] wrote:

From: [email protected] [email protected]
Subject: Re: end of sentience has ? y/n, if n please use one
“string?”=o.k.
To: “ruby-talk ML” [email protected]
Date: Thursday, March 25, 2010, 9:27 PM

On Thu, Mar 25, 2010 at 8:32 PM, jamison edmonds
[email protected] wrote:

Still new to programing.
I’m making a magic 8 ball clone, for fun and to learn by, console based for now. :slight_smile:

I need a few good examples on how to detect if input has a question mark at the end (more than one example/explanation preferably.)

So, I GETS a question in my .rb and if “string?” o.k. else I’d like to say, need a question with a ? mark at end please.

There are quite a few ways to approach this problem. If you haven’t
already, you can find an overview of various string methods here:
http://www.ruby-doc.org/core/classes/String.html

First let’s gets a string:

a = gets
abcdefg?
=> “abcdefg?\n”

I’m assuming you don’t care about the newline on the end of gets-ed
string. So, I’ll chomp it first:
http://ruby-doc.org/core/classes/String.html#M000819

a.chomp
=> “abcdefg?”
a
=> “abcdefg?\n”
a.chomp!
=> “abcdefg?”
a
=> “abcdefg?”

Strings in ruby can in some ways behave like arrays (responding to []):
http://ruby-doc.org/core/classes/String.html#M000771

substring from last (-1) position to last position (-1)

a[-1…-1]
=> “?”

substring of length 1 from last (-1) position

a[-1,1]
=> “?”

(There is also simply a method like a[-1]:
 >> a[-1]
 => 63
which returns the code for position -1, if you look 63 up in an ASCII
table, you’d see that it corresponds to ?:
 >> 63.chr
 => “?”
but, this behavior is different from ruby 1.8 and ruby 1.9, which is
why I almost didn’t mention it here.)

Strings can also be turned into arrays:
http://ruby-doc.org/core/classes/String.html#M000803

b = a.split(‘’)
=> [“a”, “b”, “c”, “d”, “e”, “f”, “g”, “?”]
b.last
=> “?”

And strings can also be matched against patterns:
http://ruby-doc.org/core/classes/String.html#M000778

does a have a ! at the end

a.match(/!$/)
=> nil

does a have a ? at the end

a.match(/?$/)
=> #<MatchData “?”>