Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Ruby CGI questions
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 16th July 19:00
kurt m. dresner
External User
 
Posts: 1
Default Ruby CGI questions



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
  Reply With Quote


  sponsored links


2 16th July 19:00
brian candler
External User
 
Posts: 1
Default Ruby CGI questions



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.
  Reply With Quote
3 16th July 19:00
mark j. reed
External User
 
Posts: 1
Default Ruby CGI questions


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 = Marshal.load(sess['deck'])

etc.

sess['deck'] = Marshal.dump(deck)

etc.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666