在来自 byebug 的 rails 中,如何以字符串形式查看会话变量的输出,仅显示其中的一部分? [英] In rails from byebug, how can I view the output of the session variable as a string, displaying only part of it?

查看:34
本文介绍了在来自 byebug 的 rails 中,如何以字符串形式查看会话变量的输出,仅显示其中的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在来自 byebug 的 rails 中,如何以字符串形式查看会话变量的输出,仅显示其中的一部分?

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

我可以从控制台查看会话变量的输出,但它真的很长.如果我可以把它放在一个字符串中并做例如thestr[1,100] .那么就可以了.但我不知道如何将它变成一个字符串.

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

如您所见,会话的响应非常长.而且我看不到如何显示,例如仅前 100 个字符.例如thestr[0,100]

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]

(再见)会话

@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"=>GET","REQUEST_PATH"=>"/", "REQUEST_URI"=>"/", "HTTP_VERSION"=>"HTTP/1.1",HTTP_HO………….....

@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 ............ ...........

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

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) 

推荐答案

这里有一个 QnA,在视图中显示会话信息? 它询问有关视图,但那里接受的答案也适用于 byebug 中的控制台

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.

那么你当然可以做 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) 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)

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

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)

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

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.

这篇关于在来自 byebug 的 rails 中,如何以字符串形式查看会话变量的输出,仅显示其中的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆