Forum: Ruby strange syntax error on rename

Posted by peteV (Guest)
on 2012-12-07 23:27
(Received via mailing list)
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
Posted by peteV (Guest)
on 2012-12-08 00:30
(Received via mailing list)
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
Posted by Matthew Kerwin (mattyk)
on 2012-12-08 00:50
(Received via mailing list)
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.
Posted by Chris Hulan (Guest)
on 2012-12-08 01:46
(Received via mailing list)
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 "#{..}"
Posted by Chris Hulan (Guest)
on 2012-12-08 01:56
(Received via mailing list)
here's how i'd do it: https://gist.github.com/4237869
Posted by peteV (Guest)
on 2012-12-08 02:17
(Received via mailing list)
On 12/07/2012 07:54 PM, Chris Hulan 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.
Posted by Chris Hulan (Guest)
on 2012-12-08 02:30
(Received via mailing list)
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,
Posted by Chris Hulan (Guest)
on 2012-12-08 02:33
(Received via mailing list)
i updated the gist, but i haven't actually run the code ;)
Posted by peteV (Guest)
on 2012-12-08 23:04
(Received via mailing list)
On 12/07/2012 08:32 PM, Chris Hulan 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 :)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.