A web proxy is not normally defined by just a port, but is usually a full host name. Charles is very likely installed on localhost. Therefore the following adjustment may work for you:
@agent ||= Mechanize.new do |agent|
agent.set_proxy("localhost", 8888)
end