Ruby CGI questions

K

Kurt M. Dresner

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 +
cgi.form {
cgi.textarea("selections") +
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
 
B

Brian Candler

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?

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.
 
M

Mark J. Reed

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.
 
R

Rasputin

* Kurt M. Dresner said:
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..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top