I also tried [ (remove-hook 'after-save-hook 'ruby-mode-set-encoding) ],
but it didn't work since, I think, the hook is buffer local?
Both add-hook and remove-hook have a LOCAL argument for modifying
the buffer-local hook value. If that's how it's added, that's also
how you can remove it (most likely via ruby-mode-hook).
And indeed if we look at ruby-mode itself this is exactly the case:
(define-derived-mode ruby-mode prog-mode "Ruby"
[...]
(add-hook 'after-save-hook #'ruby-mode-set-encoding nil 'local)
(add-hook 'electric-indent-functions #'ruby--electric-indent-p nil 'local)
(add-hook 'flymake-diagnostic-functions #'ruby-flymake-auto nil 'local)
So you would then do this:
(add-hook 'ruby-mode-hook #'my-ruby-mode-hook)
(defun my-ruby-mode-hook ()
"Custom `ruby-mode' behaviours."
(remove-hook 'after-save-hook #'ruby-mode-set-encoding 'local))
That said, I imagine ruby-mode-set-encoding is doing something useful, and you probably don't actually want to inhibit that.
This looks to me like a bug in ruby-mode-set-encoding. You should M-x report-emacs-bug to suggest that save-restriction is used in that function. Maybe there's something else going on which needs the widening to happen, but if not then it seems like an easy fix.