Session: question and problem

Hi All,

Question:
I got the impression somewhere that a session instance was created
automatically when generating a rails app. However, to initialize my
session with some empty hashes, I had to precede the initialization
with an assignment to session, a la:

class ApplicationController < ActionController::Base

session={}
%w[customers invoices payments].each { |tbl| session[tbl] = {} }

Is this the “Ruby/Rails Way”?

Problem:
I’m trying to trace my session hashes using logger.debug. I got a
complaint about using session’s each method, so I tried the following
statement in a method I defined in ApplicationController:

	logger.debug (defined? session && session.respond_to?("each") )?
		(session.each {|p| "    " + p.inspect}) :  (defined? session ?

“‘session’ doesn’t respond to ‘each’” : “‘session’ undefined” )

The message I get is:
undefined method `each’ for #CGI::Session:0x3d52190
despite the fact that I’ve got a protection against executing the
session.each clause.

It seems to me that this error is propagated at compile time, so I need
a better way to handle possibly undefined methods. Any suggestions?

Thanks in Advance,
Richard

Well, I finally realized that I really did have a syntax error, so I
recoded the offending line in a somewhat verbose way as follows:

	 if ( defined?(session) && session.respond_to?("each") )
		logger.debug "#{session.each {|p| '   ' + p.inspect}}"
	else
		if defined?(session)
			logger.debug "    Problem with session#each"
		else
			logger.debug "    'session' undefined"
		end
	end

I still do wonder about my initialization of session, however.

Regards,
Richard

Well, I finally got a succint way of doing the logging of session:

logger.debug "session.each {|p| logger.debug '   ' + p.inspect}

yields:"
session.each {|p| logger.debug ’ ’ + p.inspect}

That’s what I used to do, but I got lost in the “syntax jungle”.

I still do wonder about my initialization of session, however, as
repeated here from by original post:

Question:
I got the impression somewhere that a session instance was created
automatically when generating a rails app. However, to initialize my
session with some empty hashes, I had to precede the initialization
with an assignment to session, a la:

class ApplicationController < ActionController::Base

    session={}
    %w[customers invoices payments].each { |tbl| session[tbl] = {}

}

Is this the “Ruby/Rails Way”?

Regards,
Richard

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