[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help Please!

1up gfx

1/3/2008 7:28:00 AM

[Note: parts of this message were removed to make it a legal post.]

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!

11 Answers

Bill Kelly

1/3/2008 7:38:00 AM

0


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


Hope this helps,

Bill



Wes Bailey

1/3/2008 7:48:00 AM

0

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


1up gfx

1/3/2008 7:55:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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

Chad Perrin

1/3/2008 11:01:00 AM

0

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

Also:

ri Kernel#gets

ri Kernel#puts

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

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Rudy Giuliani: "You have free speech so I can be heard."

nox

1/3/2008 2:47:00 PM

0


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

Giles Bowkett

1/3/2008 2:57:00 PM

0

On 1/2/08, 1up gfx <jordan.rubytalk@gmail.com> 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!


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!

--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

Joel VanderWerf

1/3/2008 5:53:00 PM

0

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

Wes Bailey

1/3/2008 6:31:00 PM

0

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
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407



1up gfx

1/3/2008 7:47:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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>

Joshua Schairbaum

1/3/2008 7:59:00 PM

0

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>