0

I am brand new to Ruby and am coming from a Python background. To help learn the language, I'm porting an existing Python script so I can do a side by side comparison. Thus far, I have a small bit of code and am confused as to why 'nil' prints to the console. Code below:

#!/usr/bin/ruby
require 'date'

backup_dirs = ['/backups/db', '/backups/log']

backup_dirs.each do |backup_dir|
  Dir.glob(backup_dir + '/' + '*.*.*.*.*.*').each do |filename|
    begin
      creation_date = DateTime.strptime(filename.split('.')[3], '%m%d%Y%H%M')
    rescue
      puts "Skipping #{filename}, invalid file."
    end
  end
    puts 'File is okay.'
end

If the DateTime.strptime method throws an exception, rescue runs and puts prints out the string fine. Except, after it comes a nil. I "googled" and found that puts returns nil. But why does that show only in the rescue area and not in the File is okay. line.

I.e. an example rescue output would be:

Skipping /backups/log/fs.server.dir.999999999999.14.log, invalid file.
nil

And, how do I make it stop displaying that to the console? I'm sure this is a fundamental language thing, just very new to the language so any insight would be much appreciated.

Thanks - Tom

user164266
  • 65
  • 4
  • 1
    Could you give more details on how do you run this code? Do you copy/paste it into console? – BroiSatse Oct 03 '14 at 20:30
  • Hi sorry... left out the details. I'm just running this on the command line of a Linux server, through an SSH session... not through IRB or anything. – user164266 Oct 03 '14 at 20:37
  • Still cannot reproduce. Is that the complete code, or is it just a snippet? `nil`s never show up in the output unless you tell it (and you have to really want to see nil there, as `puts nil` will print empty line) – BroiSatse Oct 03 '14 at 20:41
  • BroiSatse ... I'm a dork, sorry :-( ... I figured it out. In the actual code, I was printing the value of creation_date not saying 'File is okay' ... it was that `puts` that was printing `nil` as it very well should have been! Sorry... first day playing with this language. Thanks so much for the help and trying to duplicate the issue. – user164266 Oct 03 '14 at 20:56
  • (Not about the nil) I think you need to insert a `next` statement after the `puts "skipping...` line, actually skipping the invalid file. – steenslag Oct 03 '14 at 21:25
  • steenslag... You are correct, that's exactly what I ended up doing. Thanks! – user164266 Oct 04 '14 at 20:12

3 Answers3

1

All... sorry. I figured this out. In the actual code I was doing a puts creation_date and that is what was causing the nil to show... as it very well should!

First day learning the language, oops! Did learn a lot about puts though... appreciate all of the answers, sorry for wasting everyones' time.

user164266
  • 65
  • 4
0

This is expected behavior. Ruby methods will return the last statement evaluated if return whatever is not specified.

In the sample you have provided you are ending with a puts statement.

puts writes your output then returns nil and subsequently your method returns nil

From irb just run the following two statements to see this in action.

puts 'foo'

'foo'

bigtunacan
  • 4,873
  • 8
  • 40
  • 73
0

This is actually because of the #puts method you're calling.

puts(obj, ...) → nil

Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.

$stdout.puts("this", "is", "a", "test") produces:

this is a test

You can check that in pry/irb:

[9] pry(main)> puts "Hello"
Hello
=> nil
Anthony
  • 15,435
  • 4
  • 39
  • 69
  • Thanks. I get it that `puts` returns `nil`. And I know we can see those when using `irb`. However, why would the `puts` in `rescue` show `nil` and the regular one does not? (This is running in a standard SSH session on Linux box). And, is there ia standard way of muting the `nil` so it doesn't show? Thanks again for the help. – user164266 Oct 03 '14 at 20:36
  • Sorry.. figured it out, my mistake, wrote details in my answer! – user164266 Oct 03 '14 at 20:59