# OptimizedImage specs are failing with Ruby 3.0.2

**URL:** https://meta.discourse.org/t/optimizedimage-specs-are-failing-with-ruby-3-0-2/204930
**Category:** Development
**Created:** [October 1, 2021, 3:45am UTC](https://meta.discourse.org/t/optimizedimage-specs-are-failing-with-ruby-3-0-2/204930 "2021-10-01T03:45:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![yahonda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yahonda/32/234118_2.png) [@yahonda](https://meta.discourse.org/u/yahonda)
#### Post date: [October 1, 2021, 3:45am UTC](https://meta.discourse.org/t/optimizedimage-specs-are-failing-with-ruby-3-0-2/204930/1 "2021-10-01T03:45:43Z")

</div>

I’m working on supporting Ruby 3 and got this spec failure.

### Steps to reproduce

1. Run Ruby 3.0.2

```plaintext
$ cd discourse
$ bundle exec rspec spec/models/optimized_image_spec.rb:10

```

### Expected result

It should finish successfully as follows

```plaintext
1 example, 0 failures

```

### Actual result

It always get failure as follows.

```plaintext
$ bundle exec rspec spec/models/optimized_image_spec.rb:10
Run options: include {:locations=>{"./spec/models/optimized_image_spec.rb"=>[10]}}

Randomized with seed 44957
F

Failures:

  1) OptimizedImage.crop should produce cropped images (requires ImageMagick 7)
     Failure/Error: expect(cropped_size).to be < 120

       expected: < 120
            got: 9476
     # ./spec/models/optimized_image_spec.rb:27:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:279:in `block (2 levels) in <top (required)>'

Finished in 0.22966 seconds (files took 2.89 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/optimized_image_spec.rb:10 # OptimizedImage.crop should produce cropped images (requires ImageMagick 7)

Randomized with seed 44957

$

```

#### Diag info

This failure is related to this line.

> <https://github.com/toy/image_optim/blob/1ed03285873078185c129c66007231469461e429/lib/image_optim/cmd.rb#L20>

When it runs with Ruby 2.7.4 it works but with Ruby 3.0.2 it raises “can’t convert ImageOptim::Timer into Float (TypeError)”. Here are the minimum steps to reproduce.

```plaintext
$ more foo.rb
gem 'image_optim'
require 'image_optim'

timeout = ImageOptim::Timer.new(15.0)

args = [{"PATH"=>
   "/usr/bin"},
   "date"
]

pid = Process.spawn(*args)
waiter = Process.detach(pid)

waiter.join(timeout)

```

- With Ruby 3.0.2, it shows “can’t convert ImageOptim::Timer into Float (TypeError)”

```plaintext
$ ruby -v
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
$ ruby foo.rb
foo.rb:14:in `join': can't convert ImageOptim::Timer into Float (TypeError)
	from foo.rb:14:in `<main>'
Fri Oct 1 12:42:51 PM JST 2021
$

```

- With Ruby 2.7.4 it works with no exception.

```plaintext
$ ruby -v
ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux]
$ ruby foo.rb
Fri Oct 1 12:44:00 PM JST 2021
$

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [October 1, 2021, 5:08am UTC](https://meta.discourse.org/t/optimizedimage-specs-are-failing-with-ruby-3-0-2/204930/2 "2021-10-01T05:08:10Z")

</div>

Looks like there is a trivial fix there … just add a `.to_f`

`if waiter.join(timeout.to_f)`

Can you send a PR through?

---

<div class="post-metadata">

### Author: ![yahonda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yahonda/32/234118_2.png) [@yahonda](https://meta.discourse.org/u/yahonda)
#### Post date: [October 1, 2021, 5:58am UTC](https://meta.discourse.org/t/optimizedimage-specs-are-failing-with-ruby-3-0-2/204930/3 "2021-10-01T05:58:30Z")

</div>

Thanks for the suggestion. Sure. Will open a pull request to [GitHub - toy/image\_optim: Optimize images using multiple utilities · GitHub](https://github.com/toy/image_optim) .

---

<div class="post-metadata">

### Author: ![yahonda](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yahonda/32/234118_2.png) [@yahonda](https://meta.discourse.org/u/yahonda)
#### Post date: [October 2, 2021, 12:21am UTC](https://meta.discourse.org/t/optimizedimage-specs-are-failing-with-ruby-3-0-2/204930/4 "2021-10-02T00:21:54Z")

</div>

Opened [Fix: TypeError: can't convert ImageOptim::Timer into Float by yahonda · Pull Request #194 · toy/image\_optim · GitHub](https://github.com/toy/image_optim/pull/194)
