Separate new lines from an output

Discussion in 'Ruby' started by Leo M., Apr 5, 2011.

  1. Leo M.

    Leo M. Guest

    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/.
    Leo M., Apr 5, 2011
    #1
    1. Advertising

  2. On Tue, Apr 5, 2011 at 11:30 AM, Leo M. <> 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/
    Robert Klemme, Apr 5, 2011
    #2
    1. Advertising

  3. Leo M.

    Haruka YAGNI Guest

    Hi Leo.

    On Tue, Apr 5, 2011 at 6:30 PM, Leo M. <> 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
    Haruka YAGNI, Apr 5, 2011
    #3
  4. Leo M.

    botp Guest

    On Tue, Apr 5, 2011 at 5:30 PM, Leo M. <> 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
    botp, Apr 5, 2011
    #4
  5. Leo M.

    Leo M. Guest

    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/.
    Leo M., Apr 5, 2011
    #5
  6. On 5 April 2011 11:38, Leo M. <> 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/.
    >
    >
    Peter Hickman, Apr 5, 2011
    #6
  7. Leo M.

    Josh Cheek Guest

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

    On Tue, Apr 5, 2011 at 5:38 AM, Leo M. <> 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.
    Josh Cheek, Apr 5, 2011
    #7
  8. Leo M.

    Leo M. Guest

    Leo M., Apr 5, 2011
    #8
  9. On Tue, Apr 5, 2011 at 12:38 PM, Leo M. <> 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.
    Phillip Gawlowski, Apr 5, 2011
    #9
  10. On Tue, Apr 5, 2011 at 12:59 PM, Peter Hickman
    <> wrote:
    > On 5 April 2011 11:38, Leo M. <> 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/
    Robert Klemme, Apr 5, 2011
    #10
  11. On 5 April 2011 13:54, Robert Klemme <> 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.
    Peter Hickman, Apr 5, 2011
    #11
  12. 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 <> wrote:
    > On 5 April 2011 13:54, Robert Klemme <> 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.
    >
    Peter Hickman, Apr 5, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    624
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    437
    Jean-Francois Briere
    Jun 27, 2006
  3. Joe Wright
    Replies:
    0
    Views:
    510
    Joe Wright
    Jul 27, 2003
  4. Replies:
    4
    Views:
    413
  5. Johnny Chang
    Replies:
    2
    Views:
    491
    Daniel Fetchinson
    Jun 5, 2009
Loading...

Share This Page