Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2316 > unrolled thread

Separate new lines from an output

Started by"Leo M." <leo.mmcm@gmail.com>
First post2011-04-05 04:30 -0500
Last post2011-04-05 07:07 -0500
Articles 12 — 7 participants

Back to article view | Back to comp.lang.ruby


Contents

  Separate new lines from an output "Leo M." <leo.mmcm@gmail.com> - 2011-04-05 04:30 -0500
    Re: Separate new lines from an output Robert Klemme <shortcutter@googlemail.com> - 2011-04-05 04:44 -0500
    Re: Separate new lines from an output Haruka YAGNI <hyagni@gmail.com> - 2011-04-05 04:47 -0500
    Re: Separate new lines from an output botp <botpena@gmail.com> - 2011-04-05 05:02 -0500
    Re: Separate new lines from an output "Leo M." <leo.mmcm@gmail.com> - 2011-04-05 05:38 -0500
      Re: Separate new lines from an output Peter Hickman <peterhickman386@googlemail.com> - 2011-04-05 05:59 -0500
        Re: Separate new lines from an output Robert Klemme <shortcutter@googlemail.com> - 2011-04-05 07:54 -0500
          Re: Separate new lines from an output Peter Hickman <peterhickman386@googlemail.com> - 2011-04-05 08:28 -0500
            Re: Separate new lines from an output Peter Hickman <peterhickman386@googlemail.com> - 2011-04-05 08:29 -0500
      Re: Separate new lines from an output Josh Cheek <josh.cheek@gmail.com> - 2011-04-05 06:36 -0500
      Re: Separate new lines from an output Phillip Gawlowski <cmdjackryan@googlemail.com> - 2011-04-05 07:33 -0500
    Re: Separate new lines from an output "Leo M." <leo.mmcm@gmail.com> - 2011-04-05 07:07 -0500

#2316 — Separate new lines from an output

From"Leo M." <leo.mmcm@gmail.com>
Date2011-04-05 04:30 -0500
SubjectSeparate new lines from an output
Message-ID<d631fa1427d24cffc6f83fbb735b4c84@ruby-forum.com>
Hello!

I've got this problem:

I've a range from 1..87.

I've got how to print 87 lines.

I'm trying to make 87 different objects, since if i try to push this
result into an array, the array.size is always 1. How could I have 87
different objects to push in an array?

Thanks a lot for your help!

L

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

[toc] | [next] | [standalone]


#2319

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-05 04:44 -0500
Message-ID<BANLkTinE52yf_9VAw2UmaY=kfjmBgHs+dQ@mail.gmail.com>
In reply to#2316
On Tue, Apr 5, 2011 at 11:30 AM, Leo M. <leo.mmcm@gmail.com> wrote:

> I've got this problem:
>
> I've a range from 1..87.
>
> I've got how to print 87 lines.
>
> I'm trying to make 87 different objects, since if i try to push this
> result into an array, the array.size is always 1. How could I have 87
> different objects to push in an array?

It's not clear what you want.  Care to show some code?

Cheers

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#2320

FromHaruka YAGNI <hyagni@gmail.com>
Date2011-04-05 04:47 -0500
Message-ID<BANLkTinfNcexy5t+0GrqC2SqZ5bD8+dPOA@mail.gmail.com>
In reply to#2316
Hi Leo.

On Tue, Apr 5, 2011 at 6:30 PM, Leo M. <leo.mmcm@gmail.com> wrote:
> I'm trying to make 87 different objects, since if i try to push this
> result into an array, the array.size is always 1. How could I have 87
> different objects to push in an array?

I might misunderstand what you want to do, but how about this?

Array.new(87) { "" }  # => ["", "", ...(87 times)]

If you want to set length of the array according to some variable.

Array.new(some_variable) { "" }

Each element of array is different.


-- 
Haruka YAGNI

[toc] | [prev] | [next] | [standalone]


#2323

Frombotp <botpena@gmail.com>
Date2011-04-05 05:02 -0500
Message-ID<BANLkTimMMJ1PtQHOcHdM2oCEDiDR3sOtYw@mail.gmail.com>
In reply to#2316
On Tue, Apr 5, 2011 at 5:30 PM, Leo M. <leo.mmcm@gmail.com> wrote:
> I'm trying to make 87 different objects, since if i try to push this
> result into an array, the array.size is always 1.

check the doc.
eg, try

   $ ri Array#"<<"


> How could I have 87
> different objects to push in an array?

many ways.
eg,

a=[]
#=> []
87.times{a<<Object.new}
#=> 87
a.size
#=> 87
a.first(5)
#=> [#<Object:0x937c16c>, #<Object:0x937c158>, #<Object:0x937c144>,
#<Object:0x937c130>, #<Object:0x937c11c>]

best regards -botp

[toc] | [prev] | [next] | [standalone]


#2325

From"Leo M." <leo.mmcm@gmail.com>
Date2011-04-05 05:38 -0500
Message-ID<b17d892bf28996db2b491fa267c98dc5@ruby-forum.com>
In reply to#2316
Sorry for being imprecise.

The matter is this : I have 87 files named black1, black2 ...... black 
87.

I want ruby to print each file with the entire path, so, this is what 
I've written :

i = 1..87

array = []

e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

array.push e

puts array.size # => 1

ruby actually prints correctly each line, but as a single array object.

I need that each line, each path printed is a single array object, so 
that in the end I'll have an array of 87 as size with :

"/R/blackout/black1"
"/R/blackout/black2"
..
"/R/blackout/black87"

I also tried the .chomp method but is pretty much the same :-\

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

[toc] | [prev] | [next] | [standalone]


#2326

FromPeter Hickman <peterhickman386@googlemail.com>
Date2011-04-05 05:59 -0500
Message-ID<BANLkTi=isj_etab5F=LH8wd3cp2KRoJODQ@mail.gmail.com>
In reply to#2325
On 5 April 2011 11:38, Leo M. <leo.mmcm@gmail.com> wrote:
> Sorry for being imprecise.
>
> The matter is this : I have 87 files named black1, black2 ...... black
> 87.
>
> I want ruby to print each file with the entire path, so, this is what
> I've written :
>
> i = 1..87
>
> array = []
>
> e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}

e at this point is an array

>
> array.push e

You have pushed an array (e) to be the first element of the array (array).

>
> puts array.size # => 1

So this is correct. You might want to try

i = 1..87

array = i.map {|i| "/R/blackout/black"+i.to_s}


puts array.size # => 87

>
> ruby actually prints correctly each line, but as a single array object.
>
> I need that each line, each path printed is a single array object, so
> that in the end I'll have an array of 87 as size with :
>
> "/R/blackout/black1"
> "/R/blackout/black2"
> ...
> "/R/blackout/black87"
>
> I also tried the .chomp method but is pretty much the same :-\
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#2334

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-05 07:54 -0500
Message-ID<BANLkTi=txhxanh7toqeG+ZCsF7eUtNNAuw@mail.gmail.com>
In reply to#2326
On Tue, Apr 5, 2011 at 12:59 PM, Peter Hickman
<peterhickman386@googlemail.com> wrote:
> On 5 April 2011 11:38, Leo M. <leo.mmcm@gmail.com> wrote:
>> Sorry for being imprecise.
>>
>> The matter is this : I have 87 files named black1, black2 ...... black
>> 87.
>>
>> I want ruby to print each file with the entire path, so, this is what
>> I've written :
>>
>> i = 1..87
>>
>> array = []
>>
>> e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}
>
> e at this point is an array

No, it's a Range.

irb(main):001:0> (1..87).each {}
=> 1..87
irb(main):002:0> (1..87).each {}.class
=> Range

>> array.push e
>
> You have pushed an array (e) to be the first element of the array (array).

He has pushed a Range into the Array.

>> puts array.size # => 1
>
> So this is correct. You might want to try
>
> i = 1..87
>
> array = i.map {|i| "/R/blackout/black"+i.to_s}
>
> puts array.size # => 87

If it is just for printing I'd rather

(1..87).each {|i| puts "/R/blackout/black#{i}"}
1.upto(87) {|i| puts "/R/blackout/black#{i}"}

Cheers

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [next] | [standalone]


#2340

FromPeter Hickman <peterhickman386@googlemail.com>
Date2011-04-05 08:28 -0500
Message-ID<BANLkTikBWR7h5EaP=6WDfnSuJ2+RvoU5Fg@mail.gmail.com>
In reply to#2334
On 5 April 2011 13:54, Robert Klemme <shortcutter@googlemail.com> wrote:
> On Tue, Apr 5, 2011 at 12:59 PM, Peter Hickman
>>> e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}
>>
>> e at this point is an array

Thank you for your correction.

[toc] | [prev] | [next] | [standalone]


#2341

FromPeter Hickman <peterhickman386@googlemail.com>
Date2011-04-05 08:29 -0500
Message-ID<BANLkTik+JF8LaiSA25tZ9hVfw8izRrv=1Q@mail.gmail.com>
In reply to#2340
Ah bugger, got my cut and paste all wrong.

Thank you, Robert Klemme, for pointing out that it was a range that
was being pushed an not an array as I stated.

On 5 April 2011 14:28, Peter Hickman <peterhickman386@googlemail.com> wrote:
> On 5 April 2011 13:54, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On Tue, Apr 5, 2011 at 12:59 PM, Peter Hickman
>>>> e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}
>>>
>>> e at this point is an array
>
> Thank you for your correction.
>

[toc] | [prev] | [next] | [standalone]


#2328

FromJosh Cheek <josh.cheek@gmail.com>
Date2011-04-05 06:36 -0500
Message-ID<BANLkTikva5KXXkk-GeXvAsDxVdS2f15kkA@mail.gmail.com>
In reply to#2325
[Note:  parts of this message were removed to make it a legal post.]

On Tue, Apr 5, 2011 at 5:38 AM, Leo M. <leo.mmcm@gmail.com> wrote:

> Sorry for being imprecise.
>
> The matter is this : I have 87 files named black1, black2 ...... black
> 87.
>
> I want ruby to print each file with the entire path, so, this is what
> I've written :
>
> i = 1..87
>
> array = []
>
> e = i.each {|i| puts ("/R/blackout/black"+i.to_s+"\n")}
>
>
Here, you are sending the path to standard output (that's what puts does,
that's what it means to print data). But you are more interested in using
the result that you are calculating rather than printing it. So you want
something that will collect those values into an array:

e = i.collect { |i| "/R/blackout/black"+i.to_s+"\n" }

(collect is another name for map)

alternatively, you could iterate over each element and add it to the array

i.each { |i| array <<  "/R/blackout/black"+i.to_s+"\n" }



Some quick advice, you probably don't want a newline in your filename, so
"/R/blackout/black"+i.to_s+"\n"
should probably be
"/R/blackout/black"+i.to_s

And it is also more common to use interpolation, because it looks nicer
(IMO), is more efficient, and requires fewer characters. It's also easier
for my brain to comprehend at a glance:
"/R/blackout/black#{i}"



> array.push e
>
> puts array.size # => 1
>
>
Something that might be helpful is the p method.

p prints out an inspected version of your object

i = 1..87

# I'll omit the output of the puts statements
e = i.each { |i| puts "/R/blackout/black#{i}" }

p e
# >> 1..87

Here, we can see that #each returns the object it was iterating over, the
range from 1 to 87, not the new array, and certainly not what we sent to
stdout.

[toc] | [prev] | [next] | [standalone]


#2330

FromPhillip Gawlowski <cmdjackryan@googlemail.com>
Date2011-04-05 07:33 -0500
Message-ID<BANLkTik3T57GTbaPiOs30H8PQFicMAbCDg@mail.gmail.com>
In reply to#2325
On Tue, Apr 5, 2011 at 12:38 PM, Leo M. <leo.mmcm@gmail.com> wrote:
> Sorry for being imprecise.
>
> The matter is this : I have 87 files named black1, black2 ...... black
> 87.
>
> I want ruby to print each file with the entire path, so, this is what
> I've written :

Why not use Dir#glob([pattern])?

That automatically creates an array with the files and their paths
that match [pattern].

-- 
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

[toc] | [prev] | [next] | [standalone]


#2329

From"Leo M." <leo.mmcm@gmail.com>
Date2011-04-05 07:07 -0500
Message-ID<2c34b0d045a05cd031040c4105b3e784@ruby-forum.com>
In reply to#2316
It works! Thanks everybody :)

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web