effect of variable

Discussion in 'Ruby' started by Jim S., Apr 19, 2011.

  1. Jim S.

    Jim S. Guest

    I am a total noob when it comes to programming. These last few days, I
    am learning ruby thanks to the book, 'beginning ruby, from novice to
    professional.' In page 96, chapter 4, developing a basic ruby app, it
    showed one other way to code to show number of lines in the file,
    'text.txt.' (*see attached file)

    text=3D''
    line_count =3D 0
    File.open("text.txt").each do |line|
    line_count +=3D 1
    text << line
    end
    puts "#{line_count} lines"

    I do not understand what the variable 'text' is for in the first, and
    fifth, lines of the code. The book explained it this way--

    "...Compared to your previous attempt, this code introduces the text
    variable and adds each line onto the end of it in turn. When the
    iteration over the file has finished=E2=80=94that is, when you run out of=

    lines=E2=80=94text contains the entire file in a single string ready for =
    you to
    use."

    Since I did not understand what the variable 'text' was for, I removed
    it to see if I'll get something different. Removing that variable did
    not change anything--I still got the same number of lines.

    line_count =3D 0
    File.open("text.txt").each do |line|
    line_count +=3D 1
    end
    puts "#{line_count} lines"

    Was the 'text' variable placed there in case there was more code to
    follow that might use that file? Removing that 'text' variable, I opened
    the file text.txt to see if the code somehow changed the file content.
    It didn't.

    Please enlighten...? Thanks!

    Attachments:
    http://www.ruby-forum.com/attachment/6132/text2.txt


    -- =

    Posted via http://www.ruby-forum.com/.=
     
    Jim S., Apr 19, 2011
    #1
    1. Advertising

  2. Jim S.

    Sam Duncan Guest

    Put the text variable back in, and try this at the end after the puts
    for the line count.

    puts text


    =]


    On 19/04/11 17:04, Jim S. wrote:
    > I am a total noob when it comes to programming. These last few days, I
    > am learning ruby thanks to the book, 'beginning ruby, from novice to
    > professional.' In page 96, chapter 4, developing a basic ruby app, it
    > showed one other way to code to show number of lines in the file,
    > 'text.txt.' (*see attached file)
    >
    > text=''
    > line_count = 0
    > File.open("text.txt").each do |line|
    > line_count += 1
    > text<< line
    > end
    > puts "#{line_count} lines"
    >
    > I do not understand what the variable 'text' is for in the first, and
    > fifth, lines of the code. The book explained it this way--
    >
    > "...Compared to your previous attempt, this code introduces the text
    > variable and adds each line onto the end of it in turn. When the
    > iteration over the file has finished—that is, when you run out of
    > lines—text contains the entire file in a single string ready for you to
    > use."
    >
    > Since I did not understand what the variable 'text' was for, I removed
    > it to see if I'll get something different. Removing that variable did
    > not change anything--I still got the same number of lines.
    >
    > line_count = 0
    > File.open("text.txt").each do |line|
    > line_count += 1
    > end
    > puts "#{line_count} lines"
    >
    > Was the 'text' variable placed there in case there was more code to
    > follow that might use that file? Removing that 'text' variable, I opened
    > the file text.txt to see if the code somehow changed the file content.
    > It didn't.
    >
    > Please enlighten...? Thanks!
    >
    > Attachments:
    > http://www.ruby-forum.com/attachment/6132/text2.txt
    >
    >
     
    Sam Duncan, Apr 19, 2011
    #2
    1. Advertising

  3. On Apr 19, 2011, at 1:04 AM, Jim S. wrote:

    > Was the 'text' variable placed there in case there was more code to
    > follow that might use that file? Removing that 'text' variable, I opened
    > the file text.txt to see if the code somehow changed the file content.
    > It didn't.



    Yep - that was there in case you wanted to use the actual text of the
    file in some way, such as searching it for a word, counting characters, etc.

    I hope you enjoy Ruby!

    Michael Edgar

    http://carboni.ca/
     
    Michael Edgar, Apr 19, 2011
    #3
  4. Jim S.

    Jim S. Guest

    Jim S., Apr 20, 2011
    #4
    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. SEWilson

    Re: Flickering effect

    SEWilson, Aug 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,100
    SEWilson
    Aug 23, 2003
  2. chan
    Replies:
    1
    Views:
    3,793
    Nico Debeuckelaere
    Mar 4, 2004
  3. darrel
    Replies:
    2
    Views:
    490
    Juan T. Llibre
    Jan 28, 2005
  4. mfglinux
    Replies:
    11
    Views:
    737
    Roberto Bonvallet
    Sep 12, 2007
  5. David Filmer
    Replies:
    19
    Views:
    267
    Kevin Collins
    May 21, 2004
Loading...

Share This Page