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

J

jamison edmonds

Hello all

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

***
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.)=A0=20

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/>=20
[
Ruby is...=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 # Output "I love Ruby"
----------------=A0=A0=A0=A0=A0=A0=A0=A0=A0 say =3D "I love Ruby"
e pluribus =A0=A0=A0 =A0=A0=A0=A0=A0=A0 puts say
unum, and such=20
:)=A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 # Outpu=
t "I *LOVE* RUBY"
=A0=A0=A0 =A0=A0=A0 =A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0 say['love'] =3D "=
*love*"
=A0=A0=A0 =A0=A0=A0 =A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0 puts say.upcase
=A0
=A0=A0=A0 =A0=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 # Output "I *love* =
Ruby"
=A0=A0=A0 =A0=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0=A0 # five times
=A0=A0=A0 =A0=A0=A0 =A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0 5.times { puts sa=
y }
]
I see I can brake string's into parts, this may help me rite?

Ruby rocks, thanks to all.
=0A=0A=0A
 
B

brabuhr

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

First let's gets a string:
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
=> "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)
=> "?"

# substring of length 1 from last (-1) position
=> "?"

(There is also simply a method like 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 ?:=> "?"
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
http://www.regular-expressions.info/ruby.html

# does a have a ! at the end=> nil
# does a have a ? at the end=> #<MatchData "?">
 
M

Michael Fellinger

Hello all

Still new to programing.
I'm making a magic 8 ball clone, for fun and to learn by, console based f= or 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 sa=
y, need a question with a ? mark at end please.
***

At <http://www.ruby-lang.org/en/>
[
Ruby is...=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 # Output "I love Ruby"
----------------=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sa= y =3D "I love Ruby"
e pluribus =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 puts s= ay
unum, and such
:)=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # Output "I *LOVE=
* RUBY"
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0 say['love'] =3D "*love*"
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0 puts say.upcase
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 # Output "I *love* Ruby"
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0 # five times
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2=A0 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 =3D Readline.readline('> ', true)
if line.end_with?('?')
puts "Consider it carefully."
else
puts "How about you ask some question?"
end
end


--=20
Michael Fellinger
CTO, The Rubyists, LLC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top