ruby rookie needs help with mtime method of File

I was looking at an example of the "mtime" method for a file and when I ran
the example I received the following traceback

C:\barry\ruby>ruby dir3-glob.rb
backtick1.rb
Traceback (most recent call last):
        2: from dir3-glob.rb:3:in `<main>'
        1: from dir3-glob.rb:3:in `glob'
dir3-glob.rb:6:in `block in <main>': undefined method `mtime' for
"backtick1.rb":String (NoMethodError)

The source of my script is as follows
     1 #!C:\Ruby\bin\ruby.exe -w
     2
     3 Dir.glob("*.rb") {|file|
     4 # do something with the file here
     5 puts file
     6 mtime = file.mtime
     7 new_filename = "#{mtime.year}-#{mtime.month}-#{mtime.day}.wma"
     8 puts "Renaming #{filename} to #{new_filename} ..."
     9 }

What have I done wrong?

···

==================

Barry Kimelman
Winnipeg, Manitoba, Canada

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

mtime is a method of the File class. However, Dir.glob doesn't pass to the
block objects of class File, but only their names. This means that your
variable file is not a File but a string. To use the mtime method, you need to
create an instance of class File corresponding to the name in the file
variable:

Dir.glob("*.rb") { |filename|
  file = File.new filename
  mtime = file.mtime
  ...
}

I hope this helps

Stefano

···

On giovedì 22 agosto 2019 21:53:31 CEST Barry Kimelman wrote:

I was looking at an example of the "mtime" method for a file and when I ran
the example I received the following traceback

C:\barry\ruby>ruby dir3-glob.rb
backtick1.rb
Traceback (most recent call last):
        2: from dir3-glob.rb:3:in `<main>'
        1: from dir3-glob.rb:3:in `glob'
dir3-glob.rb:6:in `block in <main>': undefined method `mtime' for
"backtick1.rb":String (NoMethodError)

The source of my script is as follows
     1 #!C:\Ruby\bin\ruby.exe -w
     2
     3 Dir.glob("*.rb") {|file|
     4 # do something with the file here
     5 puts file
     6 mtime = file.mtime
     7 new_filename = "#{mtime.year}-#{mtime.month}-#{mtime.day}.wma"
     8 puts "Renaming #{filename} to #{new_filename} ..."
     9 }

What have I done wrong?

Quoting Barry Kimelman (crhistopher.pike@gmail.com):

   I was looking at an example of the "mtime" method for a file and when I
   ran the example I received the following traceback
   C:\barry\ruby>ruby dir3-glob.rb
   backtick1.rb
   Traceback (most recent call last):
    2: from dir3-glob.rb:3:in `<main>'
    1: from dir3-glob.rb:3:in `glob'
   dir3-glob.rb:6:in `block in <main>': undefined method `mtime' for
   "backtick1.rb":String (NoMethodError)
   The source of my script is as follows
    1 #!C:\Ruby\bin\ruby.exe -w
    2
    3 Dir.glob("*.rb") {|file|
    4 # do something with the file here
    5 puts file
    6 mtime = file.mtime
    7 new_filename = "#{mtime.year}-#{mtime.month}-#{mtime.day}.wma"
    8 puts "Renaming #{filename} to #{new_filename} ..."
    9 }
   What have I done wrong?

mtime is a method of File, not of String

Do

mtime=File::mtime(file)

instead of

mtime = file.mtime

(then, variable 'filename' on line 8 does not exist, but that's
another bug)

Carlo

···

Subject: ruby rookie needs help with mtime method of File
  Date: gio 22 ago 19 01:53:31 -0600

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

There is also the convenient class Pathname. With that you can translate
your code to

require 'pathname'

Pathname.glob('*.rb') do |file|
  puts file
  mtime = file.mtime
  ...
end

Kind regards

robert

···

On Thu, Aug 22, 2019 at 9:14 PM Carlo E. Prelz <fluido@fluido.as> wrote:

        Subject: ruby rookie needs help with mtime method of File
        Date: gio 22 ago 19 01:53:31 -0600

Quoting Barry Kimelman (crhistopher.pike@gmail.com):

> I was looking at an example of the "mtime" method for a file and when
I
> ran the example I received the following traceback
> C:\barry\ruby>ruby dir3-glob.rb
> backtick1.rb
> Traceback (most recent call last):
> 2: from dir3-glob.rb:3:in `<main>'
> 1: from dir3-glob.rb:3:in `glob'
> dir3-glob.rb:6:in `block in <main>': undefined method `mtime' for
> "backtick1.rb":String (NoMethodError)
> The source of my script is as follows
> 1 #!C:\Ruby\bin\ruby.exe -w
> 2
> 3 Dir.glob("*.rb") {|file|
> 4 # do something with the file here
> 5 puts file
> 6 mtime = file.mtime
> 7 new_filename = "#{mtime.year}-#{mtime.month}-#{mtime.day}.wma"
> 8 puts "Renaming #{filename} to #{new_filename} ..."
> 9 }
> What have I done wrong?

mtime is a method of File, not of String

Do

mtime=File::mtime(file)

instead of

mtime = file.mtime

(then, variable 'filename' on line 8 does not exist, but that's
another bug)

Carlo

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can -
without end}
http://blog.rubybestpractices.com/