In Ruby, why aren't variables not interchangeable within code blocks?

developer098

I have a file called "file1.txt":

Ruby
programming
is fun

In files.rb, which I'm calling from IRB, I have:

File.open('file1.txt', 'r') do |file|
  while  line = file.gets
    puts "** " + line.chomp + " **" #--> why can't I use file.gets.chomp?
 end
end

Why isn't line and file.gets interchangeable on line 3? If I switch line with file.gets, the function does not work, and I am a little bit perplexed considering that

line = file.gets

and

file.gets = line

should be interchangeable, but in this case, it is not as it gives me an error. The function works with line.chomp.

I tried getting rid of the while code block, and simply writing

puts file.gets

and it seems to output a line of code from file1.txt, but does not work inside the while statement on line 3.

mhSangar

I'm not really into Ruby, but I think that is because if you use while line = file.gets, the file.gets return a line and read (and copy to buffer) the next one. In the final iteration, where the while is in the last line, the while line = file.gets will return the last line. But in the while, you call again file.gets, so as there are no more lines in file, it returns an error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why aren't variables defined within the same function but in different if/else statements nonlocal to each other?

From Dev

Ruby blocks within an array

From Dev

Why aren't Enumerators in Ruby (2.0 +) lazy by default?

From Dev

Why aren't SAS Macro Variables Local-Scope by Default?

From Dev

Swift: why aren't all variables lazy by default?

From Dev

Why aren't there existentially quantified type variables in GHC Haskell

From Dev

Why aren't my class variables being set?

From Dev

Why aren't variables like $PS1 in printenv?

From Dev

Why aren't both Double variables updated with same value?

From Dev

Buttons with very similar code aren't both working. Why?

From Dev

Why aren't these lines of code returning any result?

From Dev

Why empty functions aren't removed as dead code in LLVM IR?

From Dev

Why aren't these lines of code returning any result?

From Dev

Why pages aren't accessible after Login FE User by code?

From Dev

Ruby code blocks and Chef

From Dev

New to Ruby and SQLite , don't know why my queries aren't working

From Dev

Why aren't these vectors equal?

From Dev

why aren't primitives nullable

From Dev

Why aren't these two equal?

From Dev

Why this lines aren't working?

From Dev

Why aren't "NOT IN" and "NOT EXISTS" working?

From Dev

Why aren't these ACLs working?

From Dev

Why aren't there enough inputs?

From Dev

Why aren't all Ruby classes affected by my modification of the Object class?

From Dev

ASP.NET Core with Entity Framework - code first migrations aren't reading my environment variables

From Dev

Why aren't instance variables listed in inspect for the subclasses of built-in classes?

From Dev

Why aren't positional variables changed when one runs a command every time

From Dev

Why aren't the co-ordinate variables incrementing and decrementing in my functions in Python?

From Dev

Why aren't C++ constructors capable of adding in new member variables?

Related Related

  1. 1

    Why aren't variables defined within the same function but in different if/else statements nonlocal to each other?

  2. 2

    Ruby blocks within an array

  3. 3

    Why aren't Enumerators in Ruby (2.0 +) lazy by default?

  4. 4

    Why aren't SAS Macro Variables Local-Scope by Default?

  5. 5

    Swift: why aren't all variables lazy by default?

  6. 6

    Why aren't there existentially quantified type variables in GHC Haskell

  7. 7

    Why aren't my class variables being set?

  8. 8

    Why aren't variables like $PS1 in printenv?

  9. 9

    Why aren't both Double variables updated with same value?

  10. 10

    Buttons with very similar code aren't both working. Why?

  11. 11

    Why aren't these lines of code returning any result?

  12. 12

    Why empty functions aren't removed as dead code in LLVM IR?

  13. 13

    Why aren't these lines of code returning any result?

  14. 14

    Why pages aren't accessible after Login FE User by code?

  15. 15

    Ruby code blocks and Chef

  16. 16

    New to Ruby and SQLite , don't know why my queries aren't working

  17. 17

    Why aren't these vectors equal?

  18. 18

    why aren't primitives nullable

  19. 19

    Why aren't these two equal?

  20. 20

    Why this lines aren't working?

  21. 21

    Why aren't "NOT IN" and "NOT EXISTS" working?

  22. 22

    Why aren't these ACLs working?

  23. 23

    Why aren't there enough inputs?

  24. 24

    Why aren't all Ruby classes affected by my modification of the Object class?

  25. 25

    ASP.NET Core with Entity Framework - code first migrations aren't reading my environment variables

  26. 26

    Why aren't instance variables listed in inspect for the subclasses of built-in classes?

  27. 27

    Why aren't positional variables changed when one runs a command every time

  28. 28

    Why aren't the co-ordinate variables incrementing and decrementing in my functions in Python?

  29. 29

    Why aren't C++ constructors capable of adding in new member variables?

HotTag

Archive