在byebug的rails中,如何将会话变量的输出视为字符串,只显示其中的一部分?

问题描述 投票:-1回答:2

在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 [0100]

(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”=>“美洲狮3.12.1睡衣中的骆驼”,“GATEWAY_INTERFACE”=>“CGI / 1.2”,“REQUEST_METHOD “=>”GET“,”REQUEST_PATH“=>”/“,”REQUEST_URI“=>”/“,”HTTP_VERSION“=>”HTTP / 1.1“,”HTTP_HO ............ ...........

我尝试了session.to_s,但这使得这个字符串所以它不只是将上面的输出转换为字符串。

(byebug) session.to_s
"#<ActionDispatch::Request::Session:0x00007fa60ee91270>"
(byebug) 
ruby-on-rails session session-cookies byebug
2个回答
2
投票

您可以使用session.to_h,然后将其作为常规哈希处理。

从barlop添加了示例

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


0
投票

这里有一个QnA,Show session information in a view?它询问了一个视图,但是接受的答案也适用于byebug中的控制台

session.inspect.to_s

是您想要的输出字符串。

那么你当然可以做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>,@ detgate = {\”session_id \“=> \” 1910becce7d1a46587eede9d25e920ce \“,\”_ csrf_token \“=> \”BUEa​​rPb / jeyrHrldyY8BJhRyq9TErAG4rS00cz8aaLE = \“,\”a \“=> \”3 \“,\”godzilla \“=> \”thegodzilla \“},@ loaded =是的,@ exists = true>“ (byebug)

我认为vasfed对session.to_h的回答非常适合显示会话的变量及其相关部分..(虽然我的问题要求任何部分)

虽然这个答案显示了变量(尽管不像血管的答案那样整齐)但是这个答案在技术上回答了我的问题,这个问题要求将整个事物作为一个字符串来显示它的任何部分。

© www.soinside.com 2019 - 2024. All rights reserved.