ruby

What happens with method after `end` like `end.compact`?


In this code,

working_days = open(ARGV[0].to_s,'r').each_line.map do |line|    
  do_something
end.compact

the map function returns an array [1, nil, 3, nil]. I appended compact to the keyword end. I want to know what is behind the scene. After I add compact, does the return array become:

How can I use pry to inspect every step?

Will compact be sent with the do end block into map function ?


Solution

  • I don't understand why you are thinking it in a complicated way. It is done as:

    [1, nil, 3, nil] → [1, 3]
    

    There are no intermediate steps that you can observe.