# Why can't I do a "to\_i?"

**URL:** <https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084>\
**Category:** Ruby\
**Created:** [May 5, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084 "2010-05-05T14:44:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 5, 2010, 2:44pm UTC](https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084/1 "2010-05-05T14:44:58Z")

</div>

Hello,  
I keep getting an “undefined method `to\_f’” for this simple script. Can  
someone help me? I’m interrogating jpeg graphics files and I need to log  
their widths and depths, in picas.  
Thanks a lot,  
Peter

Dir.chdir(“T:/ioma/temp”)  
Dir.glob("\*.jpg").each do |file|  
fileinfo = `alchemy #{file} -x`  
filewidth = fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
([0-9]{1,5})[1]/)  
filedepth = fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
([0-9]{1,5})[2]/)  
fileres = fileinfo.match(/Dots per inch: [0-9]{1,5}/)

newfilewidth = (filewidth.to\_f / 200) \* 6  
newfiledepth = (filedepth.to\_f / 200) \* 6

File.open(“t:/ioma/iomafiles.log”, “a”) { |f| f.print “#{file} width:  
#{newfilewidth.to\_f.round} depth: #{newfiledepth.to\_f.round}\n” }  
end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 5, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084/2 "2010-05-05T14:46:02Z")

</div>

Sorry. I meant a “to\_f” there in the subject line, although it happens  
with to\_i, too.

Peter B. wrote:

> Hello,  
> I keep getting an “undefined method `to\_f’” for this simple script. Can  
> someone help me? I’m interrogating jpeg graphics files and I need to log  
> their widths and depths, in picas.  
> Thanks a lot,  
> Peter
> 
> Dir.chdir(“T:/ioma/temp”)  
> Dir.glob("\*.jpg").each do |file|  
> fileinfo = `alchemy #{file} -x`  
> filewidth = fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
> ([0-9]{1,5})[1]/)  
> filedepth = fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
> ([0-9]{1,5})[2]/)  
> fileres = fileinfo.match(/Dots per inch: [0-9]{1,5}/)
> 
> newfilewidth = (filewidth.to\_f / 200) \* 6  
> newfiledepth = (filedepth.to\_f / 200) \* 6
> 
> File.open(“t:/ioma/iomafiles.log”, “a”) { |f| f.print “#{file} width:  
> #{newfilewidth.to\_f.round} depth: #{newfiledepth.to\_f.round}\n” }  
> end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 5, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084/3 "2010-05-05T15:09:23Z")

</div>

to\_f occurs on three lines in the code you show us. How about a clue as  
to  
which line the error belongs to?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 5, 2010, 3:14pm UTC](https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084/4 "2010-05-05T15:14:04Z")

</div>

On Wed, May 5, 2010 at 2:45 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> filewidth = fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
> ([0-9]{1,5})[1]/)  
> filedepth = fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
> ([0-9]{1,5})[2]/)  
> fileres = fileinfo.match(/Dots per inch: [0-9]{1,5}/)

Maybe move [1] and [2] outside the regexps?

Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 5, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/why-cant-i-do-a-to-i/188084/5 "2010-05-05T15:29:53Z")

</div>

Calamitas wrote:

> On Wed, May 5, 2010 at 2:45 PM, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½filewidth ï¿½ ï¿½ ï¿½ ï¿½= fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
> > ([0-9]{1,5})[1]/)  
> > ï¿½filedepth ï¿½ ï¿½ ï¿½ ï¿½= fileinfo.match(/Width x Height: ([0-9]{1,5}) x  
> > ([0-9]{1,5})[2]/)  
> > ï¿½fileres ï¿½ ï¿½ ï¿½ ï¿½= fileinfo.match(/Dots per inch: [0-9]{1,5}/)
> 
> Maybe move [1] and [2] outside the regexps?
> 
> Peter

Yes, thanks. I found the problem. It’s the placement of my [1] and [2]  
there at the end of the lines.

Thanks for your responses.
