Writing a poem backwards or in reverse order

i would like to write a poem using nano and through ruby I would like to
do the following:

Program 1:

Reads the poem into the program and writes it to the screen backwards
from last line to first.

Program 2:

Reads the poem into the program and writes it to the screen backwards
from last line to first with the words of each line in reverse order.

Program 3:

Reads the poem into the program and prints the poem to the screen
backwards from last line to first with the words of each line in reverse
order and the characters in each word in reverse order.

I’m new to ruby and I have been trying to play around with it. Can you
help me approach this problem.

I know that I have to apply using a string called “split” in order to
write backwards from last line to first line with the words in reverse
order. Can you help me?

On Sun, Oct 16, 2011 at 2:04 AM, Teresa Nguyen
[email protected] wrote:

i would like to write a poem using nano and through ruby I would like to
do the following:

Program 1:

Reads the poem into the program and writes it to the screen backwards
from last line to first.

Use String#split to split the poem into lines. Then iterate from last
line to first line, outputting each line.

Program 2:

Reads the poem into the program and writes it to the screen backwards
from last line to first with the words of each line in reverse order.

The same as above, but also use String#split to divide each line into
words.
When outputting each line, iterate over the words in the line from
last word to first word.

Program 3:

Reads the poem into the program and prints the poem to the screen
backwards from last line to first with the words of each line in reverse
order and the characters in each word in reverse order.

The same as 2, but using String#reverse to output each word.

I’m new to ruby and I have been trying to play around with it. Can you
help me approach this problem.
I know that I have to apply using a string called “split” in order to
write backwards from last line to first line with the words in reverse
order. Can you help me?

Take a look at the String documentation, in particular:

Let us know how you progress.

Jesus.

“Jesús Gabriel y Galán” [email protected] wrote in post
#1026842:

On Sun, Oct 16, 2011 at 2:04 AM, Teresa Nguyen
[email protected] wrote:

i would like to write a poem using nano and through ruby I would like to
do the following:

Program 1:

Reads the poem into the program and writes it to the screen backwards
from last line to first.

Use String#split to split the poem into lines. Then iterate from last
line to first line, outputting each line.

Program 2:

Reads the poem into the program and writes it to the screen backwards
from last line to first with the words of each line in reverse order.

The same as above, but also use String#split to divide each line into
words.
When outputting each line, iterate over the words in the line from
last word to first word.

Program 3:

Reads the poem into the program and prints the poem to the screen
backwards from last line to first with the words of each line in reverse
order and the characters in each word in reverse order.

The same as 2, but using String#reverse to output each word.

I’m new to ruby and I have been trying to play around with it. Can you
help me approach this problem.
I know that I have to apply using a string called “split” in order to
write backwards from last line to first line with the words in reverse
order. Can you help me?

Take a look at the String documentation, in particular:

Class: String (Ruby 1.9.2)

Let us know how you progress.

Jesus.

Alright, thanks so much for your response! Can you show me an example of
this, like let’s say I have these lines:

waiting for hours
wet at first, in the cold dark
just to be gulped down

How would I go about it, using the three programs described above. I
will try to follow your example with a longer poem of my own. Thanks
again for your help! I appreciate it!

“Jesús Gabriel y Galán” [email protected] wrote in post
#1026892:

On Sun, Oct 16, 2011 at 7:15 PM, teresa nuagen
[email protected] wrote:

from last line to first.
words.

Let us know how you progress.
How would I go about it, using the three programs described above. I
will try to follow your example with a longer poem of my own. Thanks
again for your help! I appreciate it!

I guess, looking at the problem description, that you are learning
Ruby and/or programming, and that this is some kind of exercise. So it
would be wrong for me to give you a solution, since the purpose of an
excercise is to learn by trying. Let us see what you have tried. Ruby
has an interactive shell (irb) where you can try how all the functions
work, so you can learn by trying and then apply that knowledge to the
problem at hand. I recommend you take a look at these two pages:

class String - RDoc Documentation
class Array - RDoc Documentation

and get familiar with the methods in the Array and String classes.
This will help you solve many problems. In this specific case, I think
you can solve your problem using String#split, Array#reverse and
String#reverse. You will also need to know how to iterate an array:

array = [1,2,3,4,5]
array.each do |element|
puts element
end

will print
1
2
3
4
5

With this knowledge and reading carefully the documentation of split
and reverse, I think you can manage to solve the problem. If you have
any specific question about methods or you get blocked by something
you don’t understand, let us know.

Good luck !

Jesus.

alright thanks for your advice and tips. I’m familiarizing myself with
string and the order of words. Is array applied in this problem or i’m I
only focusing on string?

Can I have the poem in ruby and then change the order using separate
programs or do I have to open the file through nano first and then in
ruby? If so how do I open a nano file using ruby? My current file is
“nano poem” to open it in ruby do I have to change it into a “.txt”
file. How would I do that?

On Sun, Oct 16, 2011 at 7:15 PM, teresa nuagen
[email protected] wrote:

from last line to first.
words.

Let us know how you progress.
How would I go about it, using the three programs described above. I
will try to follow your example with a longer poem of my own. Thanks
again for your help! I appreciate it!

I guess, looking at the problem description, that you are learning
Ruby and/or programming, and that this is some kind of exercise. So it
would be wrong for me to give you a solution, since the purpose of an
excercise is to learn by trying. Let us see what you have tried. Ruby
has an interactive shell (irb) where you can try how all the functions
work, so you can learn by trying and then apply that knowledge to the
problem at hand. I recommend you take a look at these two pages:

http://www.ruby-doc.org/core/String.html
http://www.ruby-doc.org/core/Array.html

and get familiar with the methods in the Array and String classes.
This will help you solve many problems. In this specific case, I think
you can solve your problem using String#split, Array#reverse and
String#reverse. You will also need to know how to iterate an array:

array = [1,2,3,4,5]
array.each do |element|
puts element
end

will print
1
2
3
4
5

With this knowledge and reading carefully the documentation of split
and reverse, I think you can manage to solve the problem. If you have
any specific question about methods or you get blocked by something
you don’t understand, let us know.

Good luck !

Jesus.

On Sun, Oct 16, 2011 at 8:08 PM, teresa nuagen
[email protected] wrote:

alright thanks for your advice and tips. I’m familiarizing myself with
string and the order of words. Is array applied in this problem or i’m I
only focusing on string?

String#split returns an array of strings, so yes, you need to
understand arrays a bit.
An array is a container of objects, in which each element can be
accessed by index:

array = [1,2,3,4]
array[0] → 1
array[1] → 2
etc.

You can traverse an array using the code I showed above.

Can I have the poem in ruby and then change the order using separate
programs or do I have to open the file through nano first and then in
ruby? If so how do I open a nano file using ruby? My current file is
“nano poem” to open it in ruby do I have to change it into a “.txt”
file. How would I do that?

I guess your idea is to have a text file containing the original poem,
and then using Ruby to reverse the order. You can create your file
using any text editor such as nano, vi, gedit or whatever (notepad on
Windows, for example). Save it with a name that you like, for example
poem, it doesn’t need to end in .txt. Then you can read the contents
of the file using File.read, which returns a string with the contents
of the file. You can then split on “\n” (the end of line character)
and you get back an array of lines. Try this in irb:

contents = File.read(“poem”)
puts contents
lines = contents.split(“\n”)
p lines

and work from there.

Jesus.

On Sat, Oct 15, 2011 at 7:04 PM, Teresa Nguyen
[email protected]wrote:

Reads the poem into the program and writes it to the screen backwards

I know that I have to apply using a string called “split” in order to
write backwards from last line to first line with the words in reverse
order. Can you help me?


Posted via http://www.ruby-forum.com/.

Here are lots of little examples in cheatsheet format that might be
helpful:

“Jesús Gabriel y Galán” [email protected] wrote in post
#1026900:

On Sun, Oct 16, 2011 at 8:08 PM, teresa nuagen
[email protected] wrote:

alright thanks for your advice and tips. I’m familiarizing myself with
string and the order of words. Is array applied in this problem or i’m I
only focusing on string?

String#split returns an array of strings, so yes, you need to
understand arrays a bit.
An array is a container of objects, in which each element can be
accessed by index:

array = [1,2,3,4]
array[0] → 1
array[1] → 2
etc.

You can traverse an array using the code I showed above.

Can I have the poem in ruby and then change the order using separate
programs or do I have to open the file through nano first and then in
ruby? If so how do I open a nano file using ruby? My current file is
“nano poem” to open it in ruby do I have to change it into a “.txt”
file. How would I do that?

I guess your idea is to have a text file containing the original poem,
and then using Ruby to reverse the order. You can create your file
using any text editor such as nano, vi, gedit or whatever (notepad on
Windows, for example). Save it with a name that you like, for example
poem, it doesn’t need to end in .txt. Then you can read the contents
of the file using File.read, which returns a string with the contents
of the file. You can then split on “\n” (the end of line character)
and you get back an array of lines. Try this in irb:

contents = File.read(“poem”)
puts contents
lines = contents.split(“\n”)
p lines

and work from there.

Jesus.

I was able to figure out to program 1 and 3. This was the method I used
using array.

program 1:

contents = File.read(“poem”)
puts contents

my_file = File.new(“poem”)
line_array = my_file.readlines()
line_array.reverse!
line_array.each {|line| puts line}

my_file.close

program 3:

contents = File.read(“poem”)
puts contents

my_file = File.new(“poem”)
new_array = my_file.readlines()
new_array.each do |line|
puts line.reverse!
end

I’m trying to figure out how to incorporate the same method using an
array for program 2, but I keep getting an error message. Is using a
string the only way I can rearrange the order of words? Or am I tying
both arrays and string in order to do it.

On Sun, Oct 16, 2011 at 5:14 PM, teresa nuagen
[email protected]wrote:

order and the characters in each word in reverse order.


Posted via http://www.ruby-forum.com/.

This is like your program 1, but just slightly different. You will need
to
write the reverse_words function yourself, try writing it separately
from
the rest of the program, and then when you are satisfied it is correct,
use
it as shown below.

def reverse_words(line)
end

contents = File.read(“poem”)
puts contents

my_file = File.new(“poem”)
line_array = my_file.readlines()
line_array.reverse!
line_array.each { |line| puts reverse_words(line) }

my_file.close

def reverse_words(line)
end

contents = File.read(“poem”)
puts contents

my_file = File.new(“poem”)
line_array = my_file.readlines()
line_array.reverse!
line_array.each { |line| puts reverse_words(line) }

my_file.close

Alright, thank you. Is there a same method but using string?
is

def reverse_words(line)
end

included in the beginning of the file?

Josh C. wrote in post #1026925:

On Sat, Oct 15, 2011 at 7:04 PM, Teresa Nguyen
[email protected]wrote:

Reads the poem into the program and writes it to the screen backwards

I know that I have to apply using a string called “split” in order to
write backwards from last line to first line with the words in reverse
order. Can you help me?


Posted via http://www.ruby-forum.com/.

Here are lots of little examples in cheatsheet format that might be
helpful:

thanks for your response. I’ve been trying to write this program
rearranging a short poem. Can you give me an example of one method I can
use to do this:

Reads the poem into the program and prints the poem to the screen
backwards from last line to first with the words of each line in reverse
order and the characters in each word in reverse order.

Josh C. wrote in post #1026937:

On Sun, Oct 16, 2011 at 5:14 PM, teresa nuagen
[email protected]wrote:

order and the characters in each word in reverse order.


Posted via http://www.ruby-forum.com/.

This is like your program 1, but just slightly different. You will need
to
write the reverse_words function yourself, try writing it separately
from
the rest of the program, and then when you are satisfied it is correct,
use
it as shown below.

def reverse_words(line)
end

contents = File.read(“poem”)
puts contents

my_file = File.new(“poem”)
line_array = my_file.readlines()
line_array.reverse!
line_array.each { |line| puts reverse_words(line) }

my_file.close

when using this method, I got an error message that says:
poem_program2.rb:7:in block in <main>': undefined method reverse_words’ for main:Object (NoMethodError)
from poem_program2.rb:7:in each' from poem_program2.rb:7:in

Is there another way to approach the method using string and how would I
go about it to reverse and go from last line to first in reverse order?

“Jesús Gabriel y Galán” [email protected] wrote in post
#1026842:

Program 2:

Reads the poem into the program and writes it to the screen backwards
from last line to first with the words of each line in reverse order.

The same as above, but also use String#split to divide each line into
words.
When outputting each line, iterate over the words in the line from
last word to first word.

I’m struggling on how to approach the method using “string#split” I’m
unsure of how to divide each line into words and iterating it from last
word to first word, and then writing it in reverse order.

On Mon, Oct 17, 2011 at 12:25 PM, teresa nuagen
[email protected] wrote:

I’m struggling on how to approach the method using “string#split” I’m
unsure of how to divide each line into words and iterating it from last
word to first word, and then writing it in reverse order.

break down the problem into smaller ones.

eg,

s=<<-HERE
waiting for hours
wet at first, in the cold dark
just to be gulped down
HERE
#=> “waiting for hours\nwet at first, in the cold dark\njust to be
gulped down\n”

a1=s.split “\n”
#=> [“waiting for hours”, “wet at first, in the cold dark”, “just to
be gulped down”]

a2=a1.reverse
#=> [“just to be gulped down”, “wet at first, in the cold dark”,
“waiting for hours”]

a3=a2.map{|s| s.split.reverse.join(" ")}
#=> [“down gulped be to just”, “dark cold the in first, at wet”,
“hours for waiting”]

a4=a3.map{|s| s.split.map{|w| w.split(//).reverse.join}}
#=> [[“nwod”, “deplug”, “eb”, “ot”, “tsuj”], [“krad”, “dloc”, “eht”,
“ni”, “,tsrif”, “ta”, “tew”], [“sruoh”, “rof”, “gnitiaw”]]

a4=a3.map{|s| s.split.map{|w| w.split(//).reverse.join}.join(" ")}
#=> [“nwod deplug eb ot tsuj”, “krad dloc eht ni ,tsrif ta tew”,
“sruoh rof gnitiaw”]

puts a4
nwod deplug eb ot tsuj
krad dloc eht ni ,tsrif ta tew
sruoh rof gnitiaw
#=> nil

botp wrote in post #1026966:

On Mon, Oct 17, 2011 at 12:25 PM, teresa nuagen
[email protected] wrote:

I’m struggling on how to approach the method using “string#split” I’m
unsure of how to divide each line into words and iterating it from last
word to first word, and then writing it in reverse order.

break down the problem into smaller ones.

eg,

s=<<-HERE
waiting for hours
wet at first, in the cold dark
just to be gulped down
HERE
#=> “waiting for hours\nwet at first, in the cold dark\njust to be
gulped down\n”

a1=s.split “\n”
#=> [“waiting for hours”, “wet at first, in the cold dark”, “just to
be gulped down”]

a2=a1.reverse
#=> [“just to be gulped down”, “wet at first, in the cold dark”,
“waiting for hours”]

a3=a2.map{|s| s.split.reverse.join(" ")}
#=> [“down gulped be to just”, “dark cold the in first, at wet”,
“hours for waiting”]

a4=a3.map{|s| s.split.map{|w| w.split(//).reverse.join}}
#=> [[“nwod”, “deplug”, “eb”, “ot”, “tsuj”], [“krad”, “dloc”, “eht”,
“ni”, “,tsrif”, “ta”, “tew”], [“sruoh”, “rof”, “gnitiaw”]]

a4=a3.map{|s| s.split.map{|w| w.split(//).reverse.join}.join(" ")}
#=> [“nwod deplug eb ot tsuj”, “krad dloc eht ni ,tsrif ta tew”,
“sruoh rof gnitiaw”]

puts a4
nwod deplug eb ot tsuj
krad dloc eht ni ,tsrif ta tew
sruoh rof gnitiaw
#=> nil

alright, so lets say this is an example:

While moon sets
atop the trees,
leaves cling to rain.

and I have to transform it into this:
rain. to cling leaves
trees, the atop
sets moon While

The program I will be writing is a lot longer poem, so I’m just using
this short haiku as an example. Can you help me approach it using the
same method as how you explained it above?

On Mon, Oct 17, 2011 at 1:02 PM, teresa nuagen
[email protected] wrote:

alright, so lets say this is an example:

While moon sets
atop the trees,
leaves cling to rain.

and I have to transform it into this:
rain. to cling leaves
trees, the atop
sets moon While

s=<<-HERE
While moon sets
atop the trees,
leaves cling to rain.
HERE
#=> “While moon sets\natop the trees,\nleaves cling to rain.\n”

let’s split the string by newlines “\n”

the splitted components will be stored in an array

a1=s.split(“\n”)
#=> [“While moon sets”, “atop the trees,”, “leaves cling to rain.”]

now, we arrange the array in reverse order

note, you can just use the same var a1, but i’d prefer we

use another var a2 so you can back track thru the vars

a2=a1.reverse
#=> [“leaves cling to rain.”, “atop the trees,”, “While moon sets”]

stop. let’s see where we are.

puts a2
leaves cling to rain.
atop the trees,
While moon sets
#=> nil

good, we just need to arrange / reverse the words in the line

again, we just split the line by spaces where we get an array,

the call the reverse method for the array

a3=a2.split.reverse
NoMethodError: undefined method split' for ["leaves cling to rain.", "atop the trees,", "While moon sets"]:Array from (irb):22 from /usr/local/bin/irb:12:in

oops :slight_smile:

a3=a2.map{|x| x.split.reverse}
#=> [[“rain.”, “to”, “cling”, “leaves”], [“trees,”, “the”, “atop”],
[“sets”, “moon”, “While”]]

ok. i think we’re near. we just need to join the words back

a4=a3.map{|x| x.join(" ")}
#=> [“rain. to cling leaves”, “trees, the atop”, “sets moon While”]

now, let’s see

puts a4
rain. to cling leaves
trees, the atop
sets moon While
#=> nil

is that ok?

now, let’s see

puts a4
rain. to cling leaves
trees, the atop
sets moon While
#=> nil

is that ok?

yes, i tried that and it worked out great! thanks so much for your help!