sachin god
sachin god

Reputation: 685

How to convert string to YAML in Ruby 2.3.0?

I am using delayed_job in my application and I am showing some info about the jobs. I am ability show the id and priority attributes where I couldn't show the handler details.

In my view, when I try to view details of the job:

<% @jobs.each do |item| %>   
  <% obj = YAML.load(item.to_yaml) %>
  <%= obj.inspect %> 
<% end %>

when I use inspect, I am getting details as:

#<Delayed::Backend::ActiveRecord::Job id: 51, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMailer\nobject...", last_error: nil, run_at: "2016-08-25 19:56:44", locked_at: nil, failed_at: nil, locked_by: nil, created_at: "2016-08-25 19:56:44", updated_at: "2016-08-25 19:56:44", queue: nil>

Now I need to get method_name from handler, for this, inorder to list the details, I used

 <%= obj.handler.inspect %>

It gave:

"--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class 'SubscriptionNotifier'\nmethod_name: :welcome\nargs:\n- !ruby/object:User\n raw_attributes:\n deleted_at: \n name: ESPN STAR\n email: [email protected]\n encrypted_password: \"$2a$10$jlV1bljCXpto4iTHnkKVnOzE.Us6lmGDtkUVdniw4DFTk8vzkX1oS\"\n phone: ''\n website: ''\n designations: ''\n id: 22\n reset_password_token: \n reset_password_sent_at: \n remember_created_at: \n sign_in_count: '0'\n so on

For showing method_name, I think we should make above string yaml, for this I tried:

 <% obj = YAML.load(item.handler.to_yaml) %>

No luck. Please help me how can I convert to yaml or how can I show this method_name?

Upvotes: 0

Views: 2829

Answers (1)

siegy22
siegy22

Reputation: 4413

I think this should work:

<% @jobs.each do |job| %>
  <%= YAML.load(job.handler)["method_name"] %> 
<% end %>

As in job.handler the yaml is saved, you can parse this and then access the method_name with [].

Upvotes: 2

Related Questions