Self-calling method

I’m making a program to rename files based on the date/time they were
taken. My camera can take 5 fps, so I want to be able to handle
pictures taken at the same time (since the exif data isn’t exact to less
than a second)

this is what I have so far:

---------code---------------

require(‘rubygems’)
require(‘mini_exiftool’)
require(‘LIB.BBI.rb’)

def toBase36(num)
letter=‘A’
if num<10
num36 = num.to_s
else
while num>10
letter = letter.succ
num = num-1
end
num36=letter
end
return num36
end

def timecode(image)
if MiniExiftool.new(image)[‘SerialNumber’] == 620315997
owner=“JD”
end
time = MiniExiftool.new(image)[‘DateTimeOriginal’]
return to_mcode(time) +’_’+ toBase36(time.strftime(’%d’).to_i) +
toBase36(time.strftime(’%H’).to_i) +
time.strftime(’%M%S’)
end

def check(string)
if FileTest.exist?(string)
strArr = string.split(’.’)
newString = strArr.first.succ+’.’+strArr.last
check(newString)
else
finalString = string
end
return finalString
end

def renameFilesBTC(files)
files.each do |file|
ext = ‘.’+file.split(’.’).last
if file == timecode(file)+ext
puts file+’ Already Renamed’
newName = file
elsif file != timecode(file)+ext &&
FileTest.exist?(timecode(file)+ext)
puts “Mulitple Files Same Time”
newName = check(timecode(file)+‘1’+ext)
else
puts “Rename”
newName = timecode(file)+ext
end
File.rename(file , newName)
end
end


I’m testing with 3 pictures taken at the same time (actually, 3 copies
of one picture).
The first two rename fine, but the third raises


TypeError: cannot convert nil into String
from ./renameByDateTime.rb:54:in rename' from ./renameByDateTime.rb:54:inrenameFilesBTC’
from ./renameByDateTime.rb:42:in each' from ./renameByDateTime.rb:42:inrenameFilesBTC’
from (irb):3

For some reason, check() returns nil for the third image, and I can’t
see why it would do that.

Replace “check(newString)” by “finalString = check(newString)”
(line 33). Or get rid of “return finalString” (line 37).

What’s the advantage of toBase36, compared to the built-in
to_s(36)?

p toBase36(30)
p 30.to_s(36)

gegroet,
Erik V. - http://www.erikveen.dds.nl/

whaaaa, changing the base of a string? this is totally bizarre to me.

003:0> p 10.to_s
“10”
nil
004:0> p 10.to_s(14)
“a”
nil

On Sep 7, 8:51 am, “Simon S.” [email protected] wrote:

nil
004:0> p 10.to_s(14)
“a”
nil

Er, you’re not changing the base of a string. (That would be something
like 10.to_s.to_base(14).) You’re changing the number to be
represented as a string in a specific base, via an argument to the
to_s method.

ok ok. I’m glad I read that wrong, it caused me some confusion.

Erik V. wrote:

Replace “check(newString)” by “finalString = check(newString)”
(line 33). Or get rid of “return finalString” (line 37).

What’s the advantage of toBase36, compared to the built-in
to_s(36)?

p toBase36(30)
p 30.to_s(36)

gegroet,
Erik V. - http://www.erikveen.dds.nl/

I simply didn’t know about to_s(36)
(I didn’t see that in the Pickaxe)

It works! This is great, thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs