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

**URL:** <https://www.ruby-forum.com/t/end-of-sentience-has-y-n-if-n-please-use-one-string-o-k/185786>\
**Category:** Ruby\
**Created:** [March 26, 2010, 1:33am UTC](https://www.ruby-forum.com/t/end-of-sentience-has-y-n-if-n-please-use-one-string-o-k/185786 "2010-03-26T01:33:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [March 26, 2010, 1:33am UTC](https://www.ruby-forum.com/t/end-of-sentience-has-y-n-if-n-please-use-one-string-o-k/185786/1 "2010-03-26T01:33:39Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [March 26, 2010, 3:28am UTC](https://www.ruby-forum.com/t/end-of-sentience-has-y-n-if-n-please-use-one-string-o-k/185786/2 "2010-03-26T03:28:20Z")

</div>

On Thu, Mar 25, 2010 at 8:32 PM, jamison edmonds  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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](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](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](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](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](http://ruby-doc.org/core/classes/String.html#M000778)

> **[Ruby Regexp Class - Regular Expressions in Ruby](https://www.regular-expressions.info/ruby.html)**
>
> Learn how to use regular expressions with Ruby and the Ruby Regexp class

# does a have a ! at the end

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

# does a have a ? at the end

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

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [March 26, 2010, 4:14am UTC](https://www.ruby-forum.com/t/end-of-sentience-has-y-n-if-n-please-use-one-string-o-k/185786/3 "2010-03-26T04:14:00Z")

</div>

On Fri, Mar 26, 2010 at 9:32 AM, jamison edmonds  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [April 11, 2010, 2:32am UTC](https://www.ruby-forum.com/t/end-of-sentience-has-y-n-if-n-please-use-one-string-o-k/185786/4 "2010-04-11T02:32:11Z")

</div>

Thanks toÂ allÂ 🙂  
Â

— On Thu, 3/25/10, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

From: removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: end of sentience has ? y/n, if n please use one  
“string?”=o.k.  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Thursday, March 25, 2010, 9:27 PM

On Thu, Mar 25, 2010 at 8:32 PM, jamison edmonds  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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](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](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](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](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](http://ruby-doc.org/core/classes/String.html#M000778)

> **[Ruby Regexp Class - Regular Expressions in Ruby](https://www.regular-expressions.info/ruby.html)**
>
> Learn how to use regular expressions with Ruby and the Ruby Regexp class

# does a have a ! at the end

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

# does a have a ? at the end

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