Let the number of rubies be \( r \) and the number of emeralds be \( e \). The total weight of rubies and emeralds is:
\[
0.3r + 0.4e \leq 12.
\]
We want to maximize the total value:
\[
\text{Value} = 4r + 5e.
\]
Using trial and error:
- If \( r = 40 \), the total weight is \( 0.3 \times 40 = 12 \), so the bag is full. The value is \( 4 \times 40 = 160 \).
- If \( r = 20 \) and \( e = 15 \), the weight is \( 0.3 \times 20 + 0.4 \times 15 = 6 + 6 = 12 \), and the value is \( 4 \times 20 + 5 \times 15 = 80 + 75 = 155 \).
Thus, the maximum wealth is obtained by taking 40 rubies.