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

Viewed 1065

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?

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"
Related