在 byebug 的 rails 中,如何将会话变量的输出视为字符串,只显示其中的一部分?
我可以从控制台查看会话变量的输出,但它真的很长。如果我可以把它放在一个字符串中并执行例如 thestr[1,100] 。那么就可以了。但我看不出如何将它变成一个字符串。
~/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
如您所见,会话的响应非常长。而且我看不到如何仅显示前 100 个字符。例如 thestr[0,100]
(byebug) 会话
@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 穿着睡衣的骆驼", "GATEWAY_INTERFACE"=>"CGI/1.2", "REQUEST_METHOD "=>"获取","REQUEST_PATH"=>"/", "REQUEST_URI"=>"/", "HTTP_VERSION"=>"HTTP/1.1", "HTTP_HO .......... ……
我尝试了 session.to_s 但这使得这个字符串不只是将上面的输出转换为字符串。
(byebug) session.to_s
"#<ActionDispatch::Request::Session:0x00007fa60ee91270>"
(byebug)