Forums » ruby » accessing session with session.each

accessing session with session.each
Posted by Sas Sas (opensas)
on 07.04.2006 04:22
I'd like to traverse thru the contents of the session object

I've been looking at C:\ruby\lib\ruby\1.8\cgi\session.rb and found out 
that there's no each method in it.

So I tried to make accesible the data collection.

First I tried with something like this

    def each(block)
      unless @data
        @data = @dbman.restore
      end
      @data.each block
    end

I edited the file, but nothing seems to happen.

I even tried to set a breakpoint, but again nothing happens.

Is there some way to debug that file???

And then, taking into account that ruby is a fully interpreted language, 
how come my changes are being ignored???

Saludos

Sas

PS: I apologize if this is a stupid question. I'm just taking my very 
first steps with ruby and with rails.
Re: accessing session with session.each
Posted by unknown (Guest)
on 07.04.2006 04:38
On Fri, 7 Apr 2006, sas sas wrote:

>      unless @data
>
> And then, taking into account that ruby is a fully interpreted language,
> how come my changes are being ignored???
>
> Saludos
>
> Sas
>
> PS: I apologize if this is a stupid question. I'm just taking my very
> first steps with ruby and with rails.

if you are running in fastcgi mode you script, and it's dependancies, 
are
loaded only once.  is this the case?

-a
Re: accessing session with session.each
Posted by Sas Sas (opensas)
on 07.04.2006 04:46
unknown wrote:
> On Fri, 7 Apr 2006, sas sas wrote:
> 
>>      unless @data
>>
>> And then, taking into account that ruby is a fully interpreted language,
>> how come my changes are being ignored???
>>
>> Saludos
>>
>> Sas
>>
>> PS: I apologize if this is a stupid question. I'm just taking my very
>> first steps with ruby and with rails.
> 
> if you are running in fastcgi mode you script, and it's dependancies, 
> are
> loaded only once.  is this the case?
> 
> -a

I followed the install method from Agile Web Development with Rails, 
that is I'm using WEBrick, and started it with ruby script/server (well, 
in fact I'm using radrails, but I guess it's the same), so I guess I'm 
not using fastcgi.

Besides I tried reinitiating the server.

Any other idea?
Re: accessing session with session.each
Posted by unknown (Guest)
on 07.04.2006 04:59
On Fri, 7 Apr 2006, sas sas wrote:

>      unless @data
>        @data = @dbman.restore
>      end
>      @data.each block
>    end

def each(&block)
   unless @data
     @data = @dbman.restore
   end
   @data.each &block
end

-a
Re: accessing session with session.each
Posted by Sas Sas (opensas)
on 07.04.2006 05:51
thanks for the advice, but I'm stuck with something even more trivial

I tried adding the following in C:\ruby\lib\ruby\1.8\cgi\session.rb

    def sayHello
      "hello"
    end

and then in a view

<%= session.sayHello %>

but then I get the following error

 Showing app/views/resolucion/list.rhtml where line #19 raised:

undefined method `sayHello' for #<CGI::Session:0x38f6bd0>

Seems like my changes are completely ignored...

any idea???
Re: accessing session with session.each
Posted by unknown (Guest)
on 07.04.2006 06:09
On Fri, 7 Apr 2006, sas sas wrote:

> <%= session.sayHello %>
>
> but then I get the following error
>
> Showing app/views/resolucion/list.rhtml where line #19 raised:
>
> undefined method `sayHello' for #<CGI::Session:0x38f6bd0>
>
> Seems like my changes are completely ignored...
>
> any idea???

only one of three things can be happening

   - you are not using that session.rb, another one exists on your 
system

   - you are putting you edit in the wrong place in that file, show us 
the
     actual code, not a snippet

   - you are not restarting your server or otherwise loading the code

you can easily test this from the command line, which will be a whole 
lot
easier than doing from within rails.  for example

   ruby -r cgi -r cgi/session -e' s = CGI::Session.new(CGI.new); p 
s.sayHello ' < /dev/null

when this works you will have made the edit in the right place.

btw.  it's a horrible idea to change the core lib files in that way. 
ruby's
open classes provides a method about 1000 times easier and safer than 
that to
make changes to core classes:


   jib:~ > cat a.rb
   class CGI
     class Session
       def say_hello
         puts 'hi'
       end
     end
   end


   jib:~ > ruby -r cgi -r cgi/session -r ./a.rb -e' s = 
CGI::Session.new(CGI.new); s.say_hello ' < /dev/null
   hi

in otherwords you do not need to, nor should you, edit session.rb to 
make this
change.

hth.

-a
Re: accessing session with session.each
Posted by Sas Sas (opensas)
on 07.04.2006 06:11
well, I've finally solved the thing

I tried in the controller the following

class Session
  def sayHello
    "hello"
  end
end

and then, I saw the error saying something about
CGI::Session

In the C:\ruby\lib\ruby\1.8\cgi\session.rb file, the Session declaration 
wa inside the CGI class, so I had to

class CGI::Session
  def sayHello
    "hello"
  end
end

and it just worked

so I went on and defined

class CGI::Session
  def data
    unless @data
      @data = @dbman.restore
    end
    @data
  end
  def each(&block)
    data.each &block
  end
  def each_pair(&block)
    data.each_pair &block
  end
end

BTW, is there some way to redirect a bunch of methods (each, each_key, 
each_pair, each_value, empty?, to_a, etc... I guess you know what I 
mean) from self to self.data?

and then, to give it a try, in a view I added the following

<table border="1">
  <tr><th colspan="2">params</th></tr>
  <tr><th>key</th><th>value</th></tr>
<% session.each_pair { |key, value| %>
  <tr><th><%= h key %></th><th><%= h value %></th></tr>
<% } %>
</table>

Well, that's all

Saludos

Sas

PS: I'm pretty impressed with how easy is to extend ruby. Just imagine 
doing such a thing in asp...





Re: accessing session with session.each
Posted by Logan Capaldo (Guest)
on 07.04.2006 06:21
On Apr 7, 2006, at 12:11 AM, sas sas wrote:

> BTW, is there some way to redirect a bunch of methods (each, each_key,
> each_pair, each_value, empty?, to_a, etc... I guess you know what I
> mean) from self to self.data?

ri Forwardable
ri SimpleDelegator
Re: accessing session with session.each
Posted by unknown (Guest)
on 07.04.2006 06:27
On Fri, 7 Apr 2006, sas sas wrote:

> well, I've finally solved the thing

great.

> BTW, is there some way to redirect a bunch of methods (each, each_key,
> each_pair, each_value, empty?, to_a, etc... I guess you know what I
> mean) from self to self.data?

see


   http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/Forwardable.html
   http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html

hth.

-a
Re: accessing session with session.each
Posted by Sas Sas (opensas)
on 07.04.2006 07:41
Ah, one last thing

I tried to set a breakpoint on the C:\ruby\lib\ruby\1.8\cgi\session.rb 
but I couldn't do it.

I guess it's because that file is outside my app, and when I start the 
irb I run c:\work\webResolucion>ruby script\breakpointer

so, is there some way to configure the whole thing so as to be able to 
debug the rails code???

Thanks again

saludos

sas
Re: accessing session with session.each
Posted by Sas Sas (opensas)
on 07.04.2006 08:15
Well, I'm even more impressed

Just had to

class CGI::Session

  extend Forwardable
  def_delegators :data, :each, :each_pair, :length, :size

  def data
    unless @data
      @data = @dbman.restore
    end
    @data
  end
end

and that's it

saludos

Sas

PS: theres a typo in

http://www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/classes/Forwardable.html

where it says

  class RecordCollection
    extends Forwardable
    def_delegator :@records, :[], :record_number
  end

it should be "extend Forwardable" and not "extends Forwardable"

I'll send a mail to suggestions@ruby-doc.org

Saludos

Sas
Re: accessing session with session.each
Posted by sas (Guest)
on 11.04.2006 19:11
> so, is there some way to configure the whole thing so as to be able to 
> debug the rails code???

I've figured it out, just had to restart webrick

And then, debugging rails code it's just like debugging any other piece
of ruby

Saludos

Sas