Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between ENV.fetch with block or with second param?

Tags:

I'm not sure whether:

ENV.fetch("RAILS_MAX_THREADS") { 5 }

and:

ENV.fetch("RAILS_MAX_THREADS", 5)

are the same or not. What is the difference?

like image 294
Leticia Esperon Avatar asked Nov 09 '18 14:11

Leticia Esperon


1 Answers

The difference is that the missing variable name is yielded to the block.

In your example, the result is the same because you do not use the yielded string, but try this to see the difference:

ENV.fetch("RAILS_MAX_THREADS", 5)
#=> 5

ENV.fetch("RAILS_MAX_THREADS") { |missing_name| "Could not find env var named " + missing_name }
#=> "Could not find env var named RAILS_MAX_THREADS"
like image 136
ldeld Avatar answered Oct 20 '22 06:10

ldeld