Build Hash with 2 input variables
The first remark I'd like to make about your function is that the names of things are not very clear as to what it is supposed to do. Imagine presenting only the code to someone, who doesn't know what problem you are trying to solve. Do you think it would be very clear?

We can make it more general, so that it can be applied to other problems. Instead of candies and plastic bags, let's use units and containers. That way, we can also calculate practically any "number of \$x\$ in \$y\$..." problems that involve integers:


Number of boxes in delivery trucks
Number of passengers in train carts
Etc.


Since we are looking to get as many full containers as we can, and then the remainder in another container, let's name the function that way as well. Here is what it looks like so far, with just the names changed:

def get_full_containers_and_remainder(units, container_capacity)
  container_number = 1
  containers = []

  begin
    units_in_this_container = units / container_capacity &gt; 0 ? container_capacity : units
    units -= units_in_this_container
    containers &lt;&lt; [container_number, units_in_this_container]
    container_number += 1
  end while units &gt; 0
  containers.to_h
end

get_full_containers_and_remainder(120, 50) 
# {1=&gt;50, 2=&gt;50, 3=&gt;20}


So far, so good. The logic is identical, but the function is easier to understand with more general names.



The logic can be simplified to not having to rely on manually adding units one container at a time in a loop. For such small numbers this is trivial, but if you had to divide up really big numbers this would take very long. 

We can instead use a bit of simple math to calculate the remainder as well as the number of containers without a loop.

Using the modulo operator we can get the remainder:

remainder = units % container_capacity # remainder of 120 ÷ 50 = 20


Then we can calculate the number of full containers by subtracting the remainder from the total units, and then dividing by the container size. 

count_full_containers = (units - remainder) / container_capacity # (120 - 20) ÷ 50 = 2


From there it's simply a matter of adding that many full containers, and then one more container with the remainder. Note that I added a check at the beginning to return an empty hash early if either of the values is zero, to avoid division-by-zero errors.

Working demo on repl.it

def get_full_containers_and_remainder(units, container_capacity)
  if units == 0 || container_capacity == 0
    return [].to_h
  end

  remainder = units % container_capacity
  count_full_containers = (units - remainder) / container_capacity 
  container_number = 1
  containers = []

  begin
    containers &lt;&lt; [container_number, container_capacity]
    container_number += 1
  end while container_number &lt;= count_full_containers
  containers &lt;&lt; [container_number, remainder]
  return containers.to_h
end

puts get_full_containers_and_remainder(120, 50) # {1=&gt;50, 2=&gt;50, 3=&gt;20}
puts get_full_containers_and_remainder(0, 50)   # {}
puts get_full_containers_and_remainder(120, 0)  # {}