Help Please!

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!

On Jan 2, 2008, at 11:28 PM, 1up gfx wrote:

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

From: “1up gfx” [email protected]

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

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.

On Thu, Jan 03, 2008 at 04:38:25PM +0900, Bill K. wrote:

reversed.
classes and methods.

Try (from your command prompt, not from IRB)

ri String#split

ri Array#reverse

ri Array#join

Also:

ri Kernel#gets

ri Kernel#puts

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

On 1/2/08, 1up gfx [email protected] wrote:

Any help with this would be greatly appreciated!

First attempt:

[01-03 06:50] ~
! irb

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

voila!


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

Oh that’s clever! I do believe you have honors on the next tee. :wink:

Wes

On Jan 3, 2008 10:53 AM, Joel VanderWerf [email protected]
wrote:


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>

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

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 12:58 PM, Joshua S. [email protected]
wrote:

ch0wda

Golfing away…
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
guys!