It seems to me that you have a quoting error somewhere.
Try using a "here document" to output the script:
    cat -e <<__EOF >"$POST_RECEIVE_DIR"
from, to, branch = ARGF.read.split " "
# 3. Copy files to deploy directory
deploy_to_dir = File.expand_path('$DEPLOY_DIR')
GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master
puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"
__EOF
 Also, what's with the \rs? Are you trying to write DOS files?