I’m new to Ruby, and have basically NO previous coding experience in any
other language.
What I’m trying to do is create a program that will run from cmd prompt,
and
let people type in words, or paragraphs etc., and then have those words
etc.
reversed.
What I’m trying to do is create a program that will run from cmd prompt, and
let people type in words, or paragraphs etc., and then have those words etc.
reversed.
I can’t remember where exactly, but this is covered in at least one of
the basic tutorials (I know because I ran through them!). Do a google
search for “Ruby tutorial” and check some of those out.
If you can’t find it after searching let me know and I’ll try to track
it down.
gets.reverse
This is my sentence
=> “\necnetnes ym si sihT”
Second attempt:
[01-03 06:51] ~
! irb
got = gets
This is my sentence
=> “This is my sentence\n”
got.chomp! # remove the newline
=> “This is my sentence”
gots = got.split # turn it into an array
=> [“This”, “is”, “my”, “sentence”]
gots.reverse.join(" ") + “.” # reverse the array, join it with
spaces, add a dot
=> “sentence my is This.”
You don’t use irb for that. At the command prompt, that’s the ruby
command you give it.
$F = is an environmental variable that can receive split line input
as an array
a = autosplit mode, basically takes the input and splits it out
n = pauses the command, like assuming a “while” command
e = the command