在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,私有,必须重新验证”, @ 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 在睡衣中”,“ 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)
答案 0 :(得分:2)
您可以使用session.to_h
,以后再将其作为常规哈希处理。
从barlop添加了示例
(byebug)会话[:godzilla] =“ thegodzilla”
“ thegodzilla”
(byebug)session.to_h
{“ session_id” =>“ 1910becce7d1a46587eede9d25e920ce”,
“ _csrf_token” =>“ BUEarPb / jeyrHrldyY8BJhRyq9TErAG4rS00cz8aaLE =”,
“ a” =>“ 3”,“ godzilla” =>“ thegodzilla”}
(byebug)
答案 1 :(得分: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>, @delegate = {\“ session_id \” => \“ 1910becce7d1a46587eede9d25e920ce \”, \“ _ csrf_token \” => \“ BUEarPb / jeyrHrldyY8BJhRyq9TErAG4rS00cz8aaLE = \”, \“ a \” => \“ 3 \”,\“ godzilla \” => \“ thegodzilla \”},@ loaded = true, @ exists = true>“
(再见)
我认为vasfed对session.to_h的回答非常适合显示会话的变量及其相关部分。(尽管我的问题要求任何一部分)
尽管此答案显示了变量(尽管不如vasfed的答案那么巧妙),但此答案从技术上回答了我的问题,该问题要求将整个内容作为字符串显示,以显示其中的任何部分。