Forum: Ruby accessing session with session.each

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sas S. (Guest)
on 2006-04-07 06: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.
unknown (Guest)
on 2006-04-07 06:38
(Received via mailing list)
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
Sas S. (Guest)
on 2006-04-07 06: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 D. 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?
unknown (Guest)
on 2006-04-07 06:59
(Received via mailing list)
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
Sas S. (Guest)
on 2006-04-07 07: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???
unknown (Guest)
on 2006-04-07 08:09
(Received via mailing list)
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
Sas S. (Guest)
on 2006-04-07 08: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...
Logan C. (Guest)
on 2006-04-07 08:21
(Received via mailing list)
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
unknown (Guest)
on 2006-04-07 08:27
(Received via mailing list)
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/...
   http://www.ruby-doc.org/stdlib/libdoc/delegate/rdo...

hth.

-a
Sas S. (Guest)
on 2006-04-07 09: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
Sas S. (Guest)
on 2006-04-07 10: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/...

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 removed_email_address@domain.invalid

Saludos

Sas
sas (Guest)
on 2006-04-11 21: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
This topic is locked and can not be replied to.