Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3135 > unrolled thread
| Started by | "Jim S." <jimsyyap@gmail.com> |
|---|---|
| First post | 2011-04-19 00:04 -0500 |
| Last post | 2011-04-19 23:44 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.ruby
effect of variable "Jim S." <jimsyyap@gmail.com> - 2011-04-19 00:04 -0500
Re: effect of variable Sam Duncan <sduncan@wetafx.co.nz> - 2011-04-19 00:07 -0500
Re: effect of variable Michael Edgar <adgar@carboni.ca> - 2011-04-19 00:10 -0500
Re: effect of variable "Jim S." <jimsyyap@gmail.com> - 2011-04-19 23:44 -0500
| From | "Jim S." <jimsyyap@gmail.com> |
|---|---|
| Date | 2011-04-19 00:04 -0500 |
| Subject | effect of variable |
| Message-ID | <2c34b5d4acc22cb9fa2661d362cd625a@ruby-forum.com> |
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
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Sam Duncan <sduncan@wetafx.co.nz> |
|---|---|
| Date | 2011-04-19 00:07 -0500 |
| Message-ID | <4DAD18AA.5020000@wetafx.co.nz> |
| In reply to | #3135 |
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
>
>
[toc] | [prev] | [next] | [standalone]
| From | Michael Edgar <adgar@carboni.ca> |
|---|---|
| Date | 2011-04-19 00:10 -0500 |
| Message-ID | <FC3D2F1F-E89A-4A05-8835-AE1AD0D402FD@carboni.ca> |
| In reply to | #3135 |
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 adgar@carboni.ca http://carboni.ca/
[toc] | [prev] | [next] | [standalone]
| From | "Jim S." <jimsyyap@gmail.com> |
|---|---|
| Date | 2011-04-19 23:44 -0500 |
| Message-ID | <f4e13733322651a277cbf413cb3401a0@ruby-forum.com> |
| In reply to | #3135 |
thanks! -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web