Ruby quickly see what fields are in an object
Try
(result.methods - Object.public_instance_methods).sort
, where result
is the variable name of the object in question.
Although it would still be better to find the documentation for it and look there.
How to view readable version of session data object
Viewing the structure of an arbitrary object is not something that is encouraged by OOP principles: objects hide things that should be irrelevant outside them. Also, there might be more data than would be feasible to read all at once. To explore random objects, the best way might be pry
gem, which allows you to inspect objects in console. If really needed, one can rather easily create a function that would recurse over all of object's instance_variables
and print them all.
However, the generic approach is probably not needed here. This seems to be an ActionDispatch::Request::Session::Options
object. It has a #to_hash
method, and hashes can be pretty-printed:
require 'json'
puts JSON.pretty_generate(session_options.to_hash)
or if you don't need it in a standard format,
require 'pp'
pp session_options.to_hash
Ruby File.exist? returns true before file is readable
This is (very broadly and somewhat roughly) how I do it:
First, I call a post
action on the controller that is calling the background create process. This action creates a ServiceRequest
(a model in my app) with relevant service_request.details
and a status of created
. The service_request
is then sent to the background process (I use RabbitMQ). And the action returns the service_request.id
.
The front end starts pinging (via AJAX) the service request end point (something like service_requests/:id
), and the ServiceRequestController
's show
action sends back the service_request.status
(along with other stuff, including service_request.results
. This loops while the service_request.status
is neither completed
nor failed
.
Meanwhile, the background process creates the PDF. When it is done, it sets the service_request.status
to completed
. And, it sets service_request.results
to contain the data the front end needs to locate and retrieve the PDF. (I store my PDFs to and AWS bucket since I'm on Heroku.)
When the front end finally receives a service_request.status of completed
it uses the service_request.results
to fetch and display the PDF.
What is the extra data when I inspect a Date object?
What you're seeing is the output from Object.inspect
which is a human-readable representation of an object. In the case of the Date
class:
From date.rb:
# Return internal object state as a programmer-readable string.
def inspect() format('#<%s: %s,%s,%s>', self.class, @ajd, @of, @sg) end
# Return the date as a human-readable string.
#
# The format used is YYYY-MM-DD.
def to_s() strftime end
The instance variables are:
@ajd
is an Astronomical Julian Day Number@of
is an offset or fraction of a day from UTC@sg
is the Day of Calendar Reform
But what do these terms mean?
1. What is an Astronomical Julian Day Number? (@ajd)
For scientific purposes, it is convenient to refer to a date simply as a day count, counting from an arbitrary initial day. The date first chosen for this was January 1, 4713 BCE. A count of days from this date is the Julian *Day* Number or Julian *Date*. This is in local time, and counts from midnight on the initial day. The stricter usage is in UTC, and counts from midday on the initial day. This is referred to in the Date class as the Astronomical *Julian* Day *Number*. In the Date class, the Astronomical Julian Day Number includes fractional days.
2. Offset from what? (@offset)
Time zones are represented as an offset from UTC, as a fraction of a day. This offset is the how much local time is later (or earlier) than UTC. UTC offset 0 is centered on England (also known as GMT). As you travel east, the offset increases until you reach the dateline in the middle of the Pacific Ocean; as you travel west, the offset decreases.
3. What is the Day of Calendar Reform? (@sg)
The Gregorian Calendar was introduced at different times in different regions. The day on which it was introduced for a particular region is the Day *of* Calendar *Reform* for that region. This is abbreviated as
sg
(for Start of Gregorian calendar) in the Date class.
From what I can tell, the Gregorian Calendar is calendar that self-corrects via leap years.
How do I dump an object's fields to the console?
Possibly:
puts variable.inspect
Use single quote in string inspection
s = 'a"c'.inspect
s[0] = s[-1] = "'"
puts s.gsub("\\\"", "\"") #=> 'a"c'
Related Topics
How to Parse a Number from a String That May Have a Leading Zero
How to Remove All Characters in a String Until a Substring Is Matched, in Ruby
Rake: Logging Any Task Executing
How to Trigger Mouse Event in Capybara Test
How to Get Class-Object from String "A::B::C" in Ruby
In Depth Ruby Gem Development Resources (Book, Video, Sites)
How to Create a Rails 3 Route That Will Match All Requests and Direct to One Resource/Page
Remove Element at Specific Index from Redis List
Fastest Way to Find a String into an Array of String
Calling a Class Method Within a Class
Ruby on Rails Foreach with Bootstrap3 Row Class
Gem Install Rails Does Not Install Rails
Get All Local Variables or Available Methods from Irb
Ruby: Controlling Printing in Scientific Notation
Ruby on Rails Triggers Update on Serialized Attribute Every Time