-1

In rails from byebug, how can I view the output of the session variable as a string, displaying only part of it?

I can view the output of the session variable from the console but it is really long. If I could put that in a string and do e.g. thestr[1,100] . then that'd be ok. But I can't see how to get it into a string.

~/rubymac/cookiesandsessions/sessiontest1$ rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/" for 127.0.0.1 at 2019-04-24 15:34:03 +0100
Processing by ApplicationController#index as HTML
Return value is: nil

[1, 5] in /Users/apple/rubymac/cookiesandsessions/sessiontest1/app/controllers/application_controller.rb
   1: class ApplicationController < ActionController::Base
   2:  def index
   3:    byebug 
=> 4:  end
   5: end

As you can see, the response from session is really long. And I can't see how to display e.g. only the first 100 characters. e.g. thestr[0,100]

(byebug) session

@app=#>, @cache_control="max-age=0, private, must-revalidate", @no_cache_control="no-cache">>>>, @default_options={:path=>"/", :domain=>nil, :expire_after=>nil, :secure=>false, :httponly=>true, :defer=>false, :renew=>false}, @key="_sessiontest1_session", @cookie_only=true>, @req=#[1, 3], "rack.errors"=>#>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "SCRIPT_NAME"=>"", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"puma 3.12.1 Llamas in Pajamas", "GATEWAY_INTERFACE"=>"CGI/1.2", "REQUEST_METHOD"=>"GET", "REQUEST_PATH"=>"/", "REQUEST_URI"=>"/", "HTTP_VERSION"=>"HTTP/1.1", "HTTP_HO ............ ...........

I tried session.to_s but that makes this string so it doesn't just convert the above output to string.

(byebug) session.to_s
"#<ActionDispatch::Request::Session:0x00007fa60ee91270>"
(byebug) 
barlop
  • 12,887
  • 8
  • 80
  • 109
  • session.to_s[0..100] – ilan berci Apr 24 '19 at 15:08
  • @ilanberci how can that possibly work. session.to_s.length returns 54. It's a small string. I showed the output of session.to_s – barlop Apr 24 '19 at 15:12
  • look at ruby docs String... if you pass in a range.. it shows the partial string – ilan berci Apr 24 '19 at 15:15
  • @ilanberci I am well aware of doing thestr[0..100]. I don't think you are understanding what i'm saying at all. I am trying to explain to you that "abcdefg"[0..100] . is not going to be any longer than abcdefg. session.to_s is not taking the output shown by entering 'session' and converting it to string. session.to_s is already very cut down. Try reading my question where I already showed that – barlop Apr 24 '19 at 15:20

2 Answers2

4

You can use session.to_h and later handle it as a regular Hash.

Added example from barlop

(byebug) session[:godzilla]="thegodzilla"
"thegodzilla"
(byebug) session.to_h
{"session_id"=>"1910becce7d1a46587eede9d25e920ce",
"_csrf_token"=>"BUEarPb/jeyrHrldyY8BJhRyq9TErAG4rS00cz8aaLE=",
"a"=>"3", "godzilla"=>"thegodzilla"}
(byebug)

barlop
  • 12,887
  • 8
  • 80
  • 109
Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • I think this is the best way for showing what are typically the essential aspects of a session.. though it isn't flexible in showing the entire output of the session variable as string thus letting me pick whatever part, whereas my answer does so i'll accept my answer. But +1 to you, your response is very useful – barlop May 03 '19 at 22:23
0

there is a QnA here, Show session information in a view? it asks regarding a view, but the accepted answer there applies to the console in byebug too

session.inspect.to_s

is the output string you want.

so then you can of course do session.inspect.to_s[0..100]

(byebug) session.inspect.to_s.last(300)
" @port=nil, @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>, @delegate={\"session_id\"=>\"1910becce7d1a46587eede9d25e920ce\", \"_csrf_token\"=>\"BUEarPb/jeyrHrldyY8BJhRyq9TErAG4rS00cz8aaLE=\", \"a\"=>\"3\", \"godzilla\"=>\"thegodzilla\"}, @loaded=true, @exists=true>"
(byebug)

I think vasfed's answer of session.to_h is really great for showing the variables of a session, and the relevant part of it..(though my question asked for any part)

Though this answer shows the variables(albeit not as neatly as vasfed's answer), but this answer technically answers my question which asked for getting the whole thing as a string so as to show any part of it.

barlop
  • 12,887
  • 8
  • 80
  • 109