[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

self-calling method

Jonathan Denni

9/7/2007 6:06:00 AM

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:in `renameFilesBTC'
from ./renameByDateTime.rb:42:in `each'
from ./renameByDateTime.rb:42:in `renameFilesBTC'
from (irb):3
---------------------------------------

For some reason, check() returns nil for the third image, and I can't
see why it would do that.
--
Posted via http://www.ruby-....

5 Answers

Erik Veenstra

9/7/2007 10:36:00 AM

0

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.erikve...


Simon Schuster

9/7/2007 2:51:00 PM

0

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 9/7/07, Erik Veenstra <erikveen@gmail.com> 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.erikve...
>
>
>

Phrogz

9/7/2007 6:16:00 PM

0

On Sep 7, 8:51 am, "Simon Schuster" <significa...@gmail.com> wrote:
> On 9/7/07, Erik Veenstra <erikv...@gmail.com> wrote:
> > What's the advantage of toBase36, compared to the built-in
> > to_s(36)?
>
> > p toBase36(30)
> > p 30.to_s(36)
> 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

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.

Simon Schuster

9/7/2007 7:22:00 PM

0

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

On 9/7/07, Phrogz <phrogz@mac.com> wrote:
> On Sep 7, 8:51 am, "Simon Schuster" <significa...@gmail.com> wrote:
> > On 9/7/07, Erik Veenstra <erikv...@gmail.com> wrote:
> > > What's the advantage of toBase36, compared to the built-in
> > > to_s(36)?
> >
> > > p toBase36(30)
> > > p 30.to_s(36)
> > 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
>
> 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.
>
>
>

Jonathan Denni

9/10/2007 4:40:00 AM

0

Erik Veenstra 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.erikve...

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

It works! This is great, thanks.
--
Posted via http://www.ruby-....