Ruby Inspect Readability

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:

  1. @ajd is an Astronomical Julian Day Number
  2. @of is an offset or fraction of a day from UTC
  3. @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



Leave a reply



Submit