2017-04-12 6 views
1

Diese und ähnliche Probleme stoße ich die ganze Zeit, und ich würde gern den Gedankenprozess hinter dem Erhalten von Werten von Objekten verstehen, damit ich ähnliche Probleme lösen kann.Werte von Ruby-Objekten im Allgemeinen erhalten?

Wenn Sie ein ähnliches Beispiel haben, das das gleiche darstellt, dann ist das nur ein gutes.

Frage

Wie man den Wert von Content-Length zu bekommen?

Und wie sind Sie zu der Lösung gekommen?

Wenn ich pp res dann bekomme ich

#<HTTP::Message:0x0055ab1ef42738 
@http_body= 
    #<HTTP::Message::Body:0x0055ab1ef42698 
    @body= 
    "...", 
    @chunk_size=nil, 
    @positions=nil, 
    @size=0>, 
@http_header= 
    #<HTTP::Message::Headers:0x0055ab1ef42710 
    @body_charset=nil, 
    @body_date=nil, 
    @body_encoding=#<Encoding:ASCII-8BIT>, 
    @body_size=0, 
    @body_type=nil, 
    @chunked=false, 
    @dumped=false, 
    @header_item= 
    [["Connection", "close"], 
    ["Content-Type", "text/html"], 
    ["Content-Length", "291"]], 
    @http_version="1.1", 

Wenn ich pp res.methods dann sehe ich http_header, so bin ich zu denken temped, dass ich näher an Content-Length von pp res.http_header.header_item bekommen können, aber dies nicht gelingt.

Die Ausgabe von pp res.http_header.methods ist

[:[], 
:[]=, 
:dump, 
:delete, 
:add, 
:all, 
:get, 
:set, 
:request_query, 
:content_type, 
:body_encoding, 
:http_version, 
:http_version=, 
:set_headers, 
:request_uri, 
:request_absolute_uri, 
:request_absolute_uri=, 
:set_date_header, 
:request_method, 
:body_size, 
:chunked, 
:status_code, 
:reason_phrase, 
:body_type, 
:body_charset, 
:body_date, 
:init_connect_request, 
:init_request, 
:init_response, 
:status_code=, 
:content_type=, 
:contenttype, 
:contenttype=, 
:set_body_encoding, 
:body_size=, 
:create_query_uri, 
:create_query_part, 
:chunked=, 
:reason_phrase=, 
:request_uri=, 
:request_query=, 
:body_type=, 
:body_charset=, 
:body_date=, 
:methods, 
:singleton_methods, 
:protected_methods, 
:private_methods, 
:public_methods, 
:to_yaml, 
:to_yaml_properties, 
:psych_to_yaml, 
:pretty_print, 
:pretty_print_cycle, 
:pretty_print_instance_variables, 
:pretty_print_inspect, 
:instance_of?, 
:public_send, 
:instance_variable_get, 
:instance_variable_set, 
:instance_variable_defined?, 
:remove_instance_variable, 
:kind_of?, 
:instance_variables, 
:tap, 
:public_method, 
:singleton_method, 
:is_a?, 
:extend, 
:define_singleton_method, 
:method, 
:awesome_print, 
:to_enum, 
:enum_for, 
:awesome_inspect, 
:ai, 
:pretty_inspect, 
:<=>, 
:===, 
:=~, 
:!~, 
:eql?, 
:respond_to?, 
:freeze, 
:inspect, 
:display, 
:object_id, 
:send, 
:to_s, 
:nil?, 
:hash, 
:class, 
:singleton_class, 
:clone, 
:dup, 
:itself, 
:taint, 
:tainted?, 
:untaint, 
:untrust, 
:trust, 
:untrusted?, 
:frozen?, 
:!, 
:==, 
:!=, 
:__send__, 
:equal?, 
:instance_eval, 
:instance_exec, 
:__id__] 

Antwort

4

Ich weiß nicht, welche Bibliothek Sie verwenden, aber Sie können einfach mit schmutzig Instanzvariablen gehen:

arr = res.instance_variable_get(:@http_header).instance_variable_get(:@header_item) 

Jetzt suchen, einfach durch das Array:

arr.find { |a| a.first == 'Content-Length' }.last 
#=> "291" 
+0

durch die Interna eines Objekts Graben ist in der Regel keine gute Idee. Stattdessen sollten Sie die Dokumentation des Objekts überprüfen und wann immer möglich die öffentliche Schnittstelle verwenden. Normalerweise garantieren Klassen nichts über ihre interne Repräsentation. Jedes Update kann dies in irgendeiner Weise ändern, während die externe Schnittstelle durch die exponierten Methoden oft stabil bleibt. –