Setting cookies from a library?

I thought it would be nice if my login code handled session and cookie
management in a Session class, rather than explicitly in the controller.
So I made the session class attach itself to the current session:

module Hark
class Session

def self.get(session, cookiejar)
  session[:hark_session] ||=

def initialize(cookiejar)
  @cookiejar = cookiejar


Then I call it from my login action:

class HarkController < ApplicationController
def login

  hs = Hark::Session.get(session, cookies)


This works beautifully in unit tests, but when I try to run it in
I get an error when it tries to serialize the session; apparently, the
CookieJar contains an anonymous class.

Is there some way I can allow my session class to get/set cookies
passing it the CookieJar each time? On the face of it, I’d need to
store a
“pointer” to the CookieJar#[] and CookieJar#[]= functions, without
the cookiejar itself. But that’s not very Rubyish. Ideas?

Jay L.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs