ruby Questions

JP Richardson   2018-06-28 16:32

How can I “pretty” format my JSON output in Ruby on Rails?

I would like my JSON output in Ruby on Rails to be "pretty" or nicely formatted. Right now, I call to_json and my JSON is all on one line. At times this can be difficult to see if there is a ...
Dan Sinker   2018-06-30 08:57

How do I parse JSON with Ruby on Rails?

I'm looking for a simple way to parse JSON, extract a value and write it into a database in Rails. Specifically what I'm looking for, is a way to extract shortUrl from the JSON returned from the bit....
Rod   2018-06-30 09:43

Parsing a JSON string in Ruby

I have a string that I want to parse in Ruby: string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}' Is there an easy way to extract the data?
kapso   2018-06-30 12:26

How to convert a ruby hash object to JSON?

How to convert a ruby hash object to JSON? So I am trying this example below & it doesn't work? I was looking at the RubyDoc and obviously Hash object doesn't have a to_json method. But I am ...
user211662   2018-07-04 16:02

Check if a value exists in an array in Ruby

I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird']. How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more?
Dane O'Connor   2018-07-05 17:42

What does %w(array) mean?

I'm looking at the documentation for FileUtils. I'm confused by the following line: FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6' What does the %w mean? Can you point me to the ...
brainfck   2018-07-06 15:14

How to sum array of numbers in Ruby?

I have an array of integers. For example: array = [123,321,12389] Is there any nice way to get the sum of them? I know, that sum = 0 array.each { |a| sum+=a } would work.
Paul Hoffer   2018-07-06 20:03

How do I pick randomly from an array?

I want to know if there is a much cleaner way of doing this. Basically, I want to pick a random element from an array of variable length. Normally, I would do it like this: myArray = ["stuff", "...
Chris Bunch   2018-07-11 06:00

Match All Occurrences of a Regex

Is there a quick way to find every match of a regular expression in Ruby? I've looked through the Regex object in the Ruby STL and searched on Google to no avail.
Jake   2018-07-24 06:17

Rails hidden field undefined method 'merge' error

I wanna do something like this in rails Here is what I have so far in rails: <%= form_for @order do |f| %> <%= f.hidden_field :service, "test" %> <%= f.submit %> <% end %&...