Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Favor format over String#% rubocop

Tags:

ruby

rubocop

request_url ||= URI_FORMATS[:home_page] % {
          base_uri:     AppConfig.test_api['base_url'],
          end_point:    AppConfig.test_api['end_points']['home_page'],
          client_id:    AppConfig.test_api['client_id'],
        }

I am getting Favor format over String#% rubocop error for this. Any idea how to resolve this. I just gone through the cause of the error here

https://quynhcodes.wordpress.com/2017/03/29/string-interpolation-in-ruby/

But I am not using any string interpolation here.

So how can we solve this issue.?

like image 720
rubyist Avatar asked Jan 01 '23 20:01

rubyist


1 Answers

First of all, this is not about string interpolation, but about string formatting. The RuboCop Ruby style guide says:

Favor the use of sprintf and its alias format over the fairly cryptic String#% method.

This would mean changing

request_url ||= URI_FORMATS[:home_page] % {
  base_uri:  AppConfig.test_api['base_url'],
  end_point: AppConfig.test_api['end_points']['home_page'],
  client_id: AppConfig.test_api['client_id'],
}

to

request_url ||= format(
  URI_FORMATS[:home_page],
  base_uri:  AppConfig.test_api['base_url'],
  end_point: AppConfig.test_api['end_points']['home_page'],
  client_id: AppConfig.test_api['client_id'],
)
like image 138
3limin4t0r Avatar answered Jan 17 '23 22:01

3limin4t0r