Re: ruby rookie needs help with mtime method of File

Robert Klemme <[email protected]> Fri, 23 Aug 2019 19:24:19 +0200
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAM9pMnOW4TVMtiP3RA17d=oxerPrFc9W-mMfGtE0fH-XCvA5Ew@mail.gmail.com>
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 <[email protected]> wrote:

>         Subject: ruby rookie needs help with mtime method of File
>         Date: gio 22 ago 19 01:53:31 -0600
>
> Quoting Barry Kimelman ([email protected]):
>
> >    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 - [email protected]             che bisogno ci sarebbe
>   *               di parlare tanto di amore e di rettitudine? (Chuang-Tzu)
>
> Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>


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


Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>