# Self-calling method

**URL:** <https://www.ruby-forum.com/t/self-calling-method/114036>\
**Category:** Ruby\
**Created:** [September 7, 2007, 8:05am UTC](https://www.ruby-forum.com/t/self-calling-method/114036 "2007-09-07T08:05:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 7, 2007, 8:05am UTC](https://www.ruby-forum.com/t/self-calling-method/114036/1 "2007-09-07T08:05:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 7, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/self-calling-method/114036/2 "2007-09-07T12:37:18Z")

</div>

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/](http://www.erikveen.dds.nl/)

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 7, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/self-calling-method/114036/3 "2007-09-07T16:54:05Z")

</div>

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

…

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 7, 2007, 8:21pm UTC](https://www.ruby-forum.com/t/self-calling-method/114036/4 "2007-09-07T20:21:31Z")

</div>

On Sep 7, 8:51 am, “Simon S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 7, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/self-calling-method/114036/5 "2007-09-07T21:22:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jonathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jonathan](https://www.ruby-forum.com/u/jonathan)\
**Post date:** [September 10, 2007, 6:39am UTC](https://www.ruby-forum.com/t/self-calling-method/114036/6 "2007-09-10T06:39:48Z")

</div>

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/](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.
