Strange syntax error on rename

Hi,
I’m a total newb and wanted to get my hands dirty with a lil script I
could use right now.
I can get the Dir.foreach to print my directory of files separately in
irb and the rename of one existing file with it’s tag info also.
combining the two seems harder to do. Can someone please help me and
show me how to make this work together.

#!/usr/bin/ruby
require ‘taglib’

Dir.foreach("/home/music/") do |file|

Load a file

TagLib::FileRef.open (file.to_s) do |fileref|
tag = fileref.tag

tag.title
tag.artist

File.rename ( “#{file}”,"#{tag.title}-#{tag.artist}.mp3")

end # File is automatically closed at block end

On 12/07/2012 05:17 PM, peteV wrote:

File.rename ( “#{file}”,"#{tag.title}-#{tag.artist}.mp3")
^
./taglibex.rb:15: syntax error, unexpected ‘)’, expecting keyword_end

of course the error dump needed to contain the “#{tag.title}” :-[
I had copied a previous error dump by mistake from the cmd line

There’s a space between rename and the parenthesis, which is unexpected.
Usually we just leave the parens off method calls in ruby.

Sent from my phone, so excuse the typos.

I think the error is due to a missing ‘end’
you have 2 ‘do’ blocks but only 1 ‘end’

Note ‘file’ is a string so no need to ‘.to_s’ nor “#{…}”

here’s how i’d do it: gist:4237869 · GitHub

is this happening every time?
possibly some non-mp3 files in the dir?

also, Dir.foreach does not give the full path
so may have to append the path,

i updated the gist, but i haven’t actually run the code :wink:

On 12/07/2012 08:32 PM, Chris H. wrote:

also, Dir.foreach does not give the full path
so may have to append the path,

yeah some m4a files but the tag is read, I tried it with a single file
it worked with just the tag code part. so yeah I was looking at
appending the full path and tried it but strangely it does not find a
valid file.
scratching my head. I think I need to read more about the blocks and
iterators.
I’m trying to learn a bit hands on but once in a while one needs to read
a bit :slight_smile:

On 12/07/2012 07:54 PM, Chris H. wrote:

        end  # File is automatically closed at block end


    of course the error dump needed to contain the "#{tag.title}" :-[
    I had copied a previous error dump by mistake from the cmd line

thanks guys that got me passed the syntax error.

hey Chris, these are the errors I get now using your code solution

TagLib: FileRef::tag() - Called without a valid file.
TagLib: FileRef::tag() - Called without a valid file.
TagLib: FileRef::audioProperties() - Called without a valid file.