# Help Please!

**URL:** <https://rubytalk.org/t/help-please/43470>\
**Category:** ruby-talk\
**Created:** [3 January 2008 07:28 UTC](https://rubytalk.org/t/help-please/43470 "2008-01-03T07:28:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![1up\_gfx](https://avatars.discourse-cdn.com/v4/letter/1/a87d85/32.png) [@1up\_gfx](https://rubytalk.org/u/1up_gfx)\
**Post date:** [3 January 2008 07:28 UTC](https://rubytalk.org/t/help-please/43470/1 "2008-01-03T07:28:19Z")

</div>

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.

Example:

This is my sentence =\> sentence my is This

Any help with this would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [3 January 2008 07:38 UTC](https://rubytalk.org/t/help-please/43470/2 "2008-01-03T07:38:25Z")

</div>

> 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.
> 
> Example:
> 
> This is my sentence =\> sentence my is This
> 
> Any help with this would be greatly appreciated!

Do you have `ri` installed on your system?

ri is a program which can provide help with Ruby's core classes and methods.

Try (from your command prompt, not from IRB)

ri String#split

ri Array#reverse

ri Array#join

Hope this helps,

Bill

> **···**
>
> From: "1up gfx" \<jordan.rubytalk@gmail.com\>

---

<div class="post-metadata">

**Author:** ![Wes\_Bailey](https://avatars.discourse-cdn.com/v4/letter/w/c57346/32.png) [@Wes\_Bailey](https://rubytalk.org/u/Wes_Bailey)\
**Post date:** [3 January 2008 07:48 UTC](https://rubytalk.org/t/help-please/43470/3 "2008-01-03T07:48:24Z")

</div>

~\> ruby -e 'ARGF.each { |l| puts l.split.reverse.join( " " ) }'  
this is my sentence  
sentence my is this

Its a great answer to your assignment if you can explain what all is happening. Good Luck!

Wes

> **···**
>
> On Jan 2, 2008, at 11:28 PM, 1up gfx wrote:
> 
> > 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.
> > 
> > Example:
> > 
> > This is my sentence =\> sentence my is This
> > 
> > Any help with this would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![nox](https://avatars.discourse-cdn.com/v4/letter/n/b4bc9f/32.png) [@nox](https://rubytalk.org/u/nox)\
**Post date:** [3 January 2008 14:49 UTC](https://rubytalk.org/t/help-please/43470/4 "2008-01-03T14:49:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Giles\_Bowkett](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@Giles\_Bowkett](https://rubytalk.org/u/Giles_Bowkett)\
**Post date:** [3 January 2008 14:57 UTC](https://rubytalk.org/t/help-please/43470/5 "2008-01-03T14:57:28Z")

</div>

> 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.
> 
> Example:
> 
> This is my sentence =\> sentence my is This
> 
> Any help with this would be greatly appreciated!

First attempt:

\<macbook of doom:giles\> [01-03 06:50] ~  
! irb

> > gets.reverse

This is my sentence  
=\> "\necnetnes ym si sihT"

Second attempt:

\<macbook of doom:giles\> [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."

voila!

> **···**
>
> On 1/2/08, 1up gfx \<jordan.rubytalk@gmail.com\> wrote:
> 
> --  
> Giles Bowkett
> 
> Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![1up\_gfx](https://avatars.discourse-cdn.com/v4/letter/1/a87d85/32.png) [@1up\_gfx](https://rubytalk.org/u/1up_gfx)\
**Post date:** [3 January 2008 07:54 UTC](https://rubytalk.org/t/help-please/43470/6 "2008-01-03T07:54:31Z")

</div>

W

> **···**
>
> On Jan 3, 2008 12:48 AM, Wes Bailey \<wes@verticalresponse.com\> wrote:
> 
> > On Jan 2, 2008, at 11:28 PM, 1up gfx wrote:
> > 
> > \> 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.  
> > \>  
> > \> Example:  
> > \>  
> > \> This is my sentence =\> sentence my is This  
> > \>  
> > \> Any help with this would be greatly appreciated!
> > 
> > ~\> ruby -e 'ARGF.each { |l| puts l.split.reverse.join( " " ) }'  
> > this is my sentence  
> > sentence my is this
> > 
> > Its a great answer to your assignment if you can explain what all is  
> > happening. Good Luck!
> > 
> > Wes
> > 
> > Well, It's not really an assignment. I'm getting my cousin (who is a Java  
> > programmer) to help me out with programming. He's basically just giving me  
> > little tasks to do so that I become more familiar with programming. But,  
> > since I don't really have any previous experience, I don't really know what  
> > to do half of the time. Thanks for your replies though.

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin1](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Chad\_Perrin1](https://rubytalk.org/u/Chad_Perrin1)\
**Post date:** [3 January 2008 11:01 UTC](https://rubytalk.org/t/help-please/43470/7 "2008-01-03T11:01:06Z")

</div>

Also:

&nbsp;&nbsp;ri Kernel#gets

&nbsp;&nbsp;ri Kernel#puts

. . . and, aside from basic Ruby syntax, that might be everything you  
need for the program.

> **···**
>
> On Thu, Jan 03, 2008 at 04:38:25PM +0900, Bill Kelly wrote:
> 
> > From: "1up gfx" \<jordan.rubytalk@gmail.com\>  
> > \>  
> > \>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.  
> > \>  
> > \>Example:  
> > \>  
> > \>This is my sentence =\> sentence my is This  
> > \>  
> > \>Any help with this would be greatly appreciated!
> > 
> > Do you have `ri` installed on your system?
> > 
> > ri is a program which can provide help with Ruby's core  
> > classes and methods.
> > 
> > Try (from your command prompt, not from IRB)
> > 
> > ri String#split
> > 
> > ri Array#reverse
> > 
> > ri Array#join
> 
> --  
> CCD CopyWrite Chad Perrin [[http://ccd.apotheon.org](http://ccd.apotheon.org) ]  
> Rudy Giuliani: "You have free speech so I can be heard."

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [3 January 2008 17:53 UTC](https://rubytalk.org/t/help-please/43470/8 "2008-01-03T17:53:22Z")

</div>

Wes Bailey wrote:

> ~\> ruby -e 'ARGF.each { |l| puts l.split.reverse.join( " " ) }'  
> this is my sentence  
> sentence my is this

Golfing away...

$ ruby -ane 'puts $F.reverse.join(" ")'  
1 2 3  
3 2 1

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Wes\_Bailey](https://avatars.discourse-cdn.com/v4/letter/w/c57346/32.png) [@Wes\_Bailey](https://rubytalk.org/u/Wes_Bailey)\
**Post date:** [3 January 2008 18:31 UTC](https://rubytalk.org/t/help-please/43470/9 "2008-01-03T18:31:17Z")

</div>

Oh that's clever! I do believe you have honors on the next tee. 😉

Wes

> **···**
>
> > Golfing away...
> > 
> > $ ruby -ane 'puts $F.reverse.join(" ")'  
> > 1 2 3  
> > 3 2 1
> > 
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![1up\_gfx](https://avatars.discourse-cdn.com/v4/letter/1/a87d85/32.png) [@1up\_gfx](https://rubytalk.org/u/1up_gfx)\
**Post date:** [3 January 2008 19:46 UTC](https://rubytalk.org/t/help-please/43470/10 "2008-01-03T19:46:57Z")

</div>

irb(main):001:0\> puts $F.reverse.join(" ")  
NoMethodError: undefined method `reverse' for nil:NilClass  
&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1  
irb(main):002:0\>

> **···**
>
> On Jan 3, 2008 10:53 AM, Joel VanderWerf \<vjoel@path.berkeley.edu\> wrote:
> 
> > Wes Bailey wrote:  
> > \> ~\> ruby -e 'ARGF.each { |l| puts l.split.reverse.join( " " ) }'  
> > \> this is my sentence  
> > \> sentence my is this
> > 
> > Golfing away...
> > 
> > $ ruby -ane 'puts $F.reverse.join(" ")'  
> > 1 2 3  
> > 3 2 1
> > 
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
> > 
> > It doesn't like your code... I keep on getting errors:

---

<div class="post-metadata">

**Author:** ![Joshua\_Schairbaum](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@Joshua\_Schairbaum](https://rubytalk.org/u/Joshua_Schairbaum)\
**Post date:** [3 January 2008 19:58 UTC](https://rubytalk.org/t/help-please/43470/11 "2008-01-03T19:58:45Z")

</div>

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

regards,  
ch0wda

> **···**
>
> On Jan 3, 2008, at 2:46 PM, 1up gfx wrote:
> 
> > On Jan 3, 2008 10:53 AM, Joel VanderWerf \<vjoel@path.berkeley.edu\> \> wrote:
> > 
> > > Wes Bailey wrote:
> > > 
> > > > ~\> ruby -e 'ARGF.each { |l| puts l.split.reverse.join( " " ) }'  
> > > > this is my sentence  
> > > > sentence my is this
> > > 
> > > Golfing away...
> > > 
> > > $ ruby -ane 'puts $F.reverse.join(" ")'  
> > > 1 2 3  
> > > 3 2 1
> > > 
> > > --  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
> > > 
> > > It doesn't like your code... I keep on getting errors:
> > 
> > irb(main):001:0\> puts $F.reverse.join(" ")  
> > NoMethodError: undefined method `reverse' for nil:NilClass  
> > &nbsp;&nbsp;&nbsp;&nbsp;from (irb):1  
> > irb(main):002:0\>

---

<div class="post-metadata">

**Author:** ![1up\_gfx](https://avatars.discourse-cdn.com/v4/letter/1/a87d85/32.png) [@1up\_gfx](https://rubytalk.org/u/1up_gfx)\
**Post date:** [3 January 2008 23:23 UTC](https://rubytalk.org/t/help-please/43470/12 "2008-01-03T23:23:51Z")

</div>

guys!

> **···**
>
> On Jan 3, 2008 12:58 PM, Joshua Schairbaum \<joshua.schairbaum@gmail.com\> wrote:
> 
> > 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
> > 
> > regards,  
> > ch0wda  
> > On Jan 3, 2008, at 2:46 PM, 1up gfx wrote:
> > 
> > \> On Jan 3, 2008 10:53 AM, Joel VanderWerf \<vjoel@path.berkeley.edu\> \> \> wrote:  
> > \>  
> > \>\> Wes Bailey wrote:  
> > \>\>\> ~\> ruby -e 'ARGF.each { |l| puts l.split.reverse.join( " " ) }'  
> > \>\>\> this is my sentence  
> > \>\>\> sentence my is this  
> > \>\>  
> > \>\> Golfing away...  
> > \>\>  
> > \>\> $ ruby -ane 'puts $F.reverse.join(" ")'  
> > \>\> 1 2 3  
> > \>\> 3 2 1  
> > \>\>  
> > \>\> --  
> > \>\> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407  
> > \>\>  
> > \>\> It doesn't like your code... I keep on getting errors:  
> > \>  
> > \> irb(main):001:0\> puts $F.reverse.join(" ")  
> > \> NoMethodError: undefined method `reverse' for nil:NilClass  
> > \> from (irb):1  
> > \> irb(main):002:0\>
> > 
> > Alright, never mind, I got the program to work. Thank you for your help
