问题
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)
回答1:
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)
回答2:
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.
来源:https://stackoverflow.com/questions/55833240/in-rails-from-byebug-how-can-i-view-the-output-of-the-session-variable-as-a-str