[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby CGI questions

Kurt M. Dresner

7/14/2003 9:12:00 PM

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

3 Answers

Brian Candler

7/14/2003 9:48:00 PM

0

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?

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.

Mark J. Reed

7/14/2003 9:55:00 PM

0

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.



Rasputin

7/14/2003 10:51:00 PM

0

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