# Ruby CGI questions

**URL:** <https://rubytalk.org/t/ruby-cgi-questions/6847>\
**Category:** ruby-talk\
**Created:** [14 July 2003 21:11 UTC](https://rubytalk.org/t/ruby-cgi-questions/6847 "2003-07-14T21:11:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [14 July 2003 21:11 UTC](https://rubytalk.org/t/ruby-cgi-questions/6847/1 "2003-07-14T21:11:53Z")

</div>

I’m trying to make a game (or at least something, so I can learn) with  
Ruby CGI. I’m having a really hard time with sessions. I have some  
variables that I want to save, but they are not strings. They are  
classes that I have defined. Is there any way to save them with a  
session without them just being converted to strings and then being read  
back in as strings when I load the session up? Here’s what I have:

#! /usr/bin/env ruby

require "cgi"  
require "cgi/session"  
require "setdeck"  
require "set"  
require "setcollection"  
require “setboard”

cgi = CGI.new(“html4”)  
sess = CGI::Session.new( cgi, “session\_key” =\> “crappyset”,  
“session\_id” =\> “1234”, #cgi.remote\_addr,  
“prefix” =\> “setgame.”,  
“tmpdir” =\> “/tmp”)

if sess[“deck”].nil?  
deck = SetDeck.new  
sets = SetCollection.new  
board = SetBoard.new(12)  
deck.shuffle!  
else  
deck = sess[“deck”]  
sets = sess[“sets”]  
board = sess[“board”]  
end

board.place(deck.dealn(board.holes.size))

if cgi.has\_key?(‘selections’)  
tomark = cgi[‘selections’][0].split(’,’).collect { |x| x.to\_i }  
tomark.each do |marked|  
board.select(marked)  
end  
end

sess[“deck”] = deck  
sess[“sets”] = sets  
sess[“board”] = board  
sess.update

cgi.out{  
cgi.html {  
cgi.head {"\n" + cgi.title {“What a crappy implementation of set!”}}

> **···**
>
> - cgi.body {"\n" + cgi.pre{board.to\_s} +  
> [cgi.br](http://cgi.br) +  
> cgi.form {  
> cgi.textarea(“selections”) +  
> [cgi.br](http://cgi.br) +  
> cgi.submit(“Check them!”)  
> }  
> }  
> }  
> }
> 
> I am just getting started with this, but I’m trying to learn how  
> sessions work and having a hell of a time. I need to make sure that  
> when deck, sets, and board are loaded up, they are loaded as a SetDeck,  
> a SetCollection, and a SetBoard, respectively. Is there any way to do  
> this without using Marshal? And if I need to use Marshal, how would I  
> do that with sessions?
> 
> -Kurt

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [14 July 2003 21:48 UTC](https://rubytalk.org/t/ruby-cgi-questions/6847/2 "2003-07-14T21:48:19Z")

</div>

I haven’t used the CGI::Session module, but you may or may not be aware of  
Marshal which can convert (almost) any object into a string representation  
and back again. So perhaps all you’re looking for is:

sess[“deck”] = Marshal.dump(deck)

and

deck = Marshal.load(sess[“deck”])

(although it’s not clear to me why CGI::Session doesn’t do that for you)

Regards,

Brian.

> **···**
>
> On Tue, Jul 15, 2003 at 06:11:53AM +0900, Kurt M. Dresner wrote:
> 
> > I’m trying to make a game (or at least something, so I can learn) with  
> > Ruby CGI. I’m having a really hard time with sessions. I have some  
> > variables that I want to save, but they are not strings. They are  
> > classes that I have defined. Is there any way to save them with a  
> > session without them just being converted to strings and then being read  
> > back in as strings when I load the session up?

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [14 July 2003 21:54 UTC](https://rubytalk.org/t/ruby-cgi-questions/6847/3 "2003-07-14T21:54:53Z")

</div>

CGI::Session variables are stored as strings, because one of the ways  
they get stored is in browser cookies. So if you need to save and  
restore complex objects, you need to serialize them with Marshal.

> deck = sess[“deck”]  
> deck = Marshal.load(sess[‘deck’])

etc.

> sess[“deck”] = deck  
> sess[‘deck’] = Marshal.dump(deck)

etc.

---

<div class="post-metadata">

**Author:** ![Rasputin1](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@Rasputin1](https://rubytalk.org/u/Rasputin1)\
**Post date:** [14 July 2003 22:50 UTC](https://rubytalk.org/t/ruby-cgi-questions/6847/4 "2003-07-14T22:50:31Z")

</div>

- Kurt M. Dresner [kdresner@cs.utexas.edu](mailto:kdresner@cs.utexas.edu) [0712 22:12]:

> I’m trying to make a game (or at least something, so I can learn) with  
> Ruby CGI. I’m having a really hard time with sessions. I have some  
> variables that I want to save, but they are not strings. They are  
> classes that I have defined. Is there any way to save them with a  
> session without them just being converted to strings and then being read  
> back in as strings when I load the session up? Here’s what I have:

Try cgi-session-PStore in RAA. Found it this afternoon…

> **···**
>
> > #! /usr/bin/env ruby
> > 
> > require “cgi”  
> > require “cgi/session”  
> > require “setdeck”  
> > require “set”  
> > require “setcollection”  
> > require “setboard”
> > 
> > cgi = CGI.new(“html4”)  
> > sess = CGI::Session.new( cgi, “session\_key” =\> “crappyset”,  
> > “session\_id” =\> “1234”, #cgi.remote\_addr,  
> > “prefix” =\> “setgame.”,  
> > “tmpdir” =\> “/tmp”)
> 
> –  
> Only adults have difficulty with childproof caps.  
> Rasputin :: Jack of All Trades - Master of Nuns
