Inspecting a hash - ruby

When I want to debug the following hash, it returns try2test2.
dictionary = {
"test" => 2,
"try" => 2
}
puts dictionary
# => try2test2
Are there any other ways to do it so that it will give you the full list like {'test': 2, 'try': 2}?

As V. Melnychuk mentioned, JSON is a good option, just remember to import the "json" module first:
require "json"
dictionary.to_json
in general, you can retreive a readable string version of an object by calling
inspect on it:
dictionary.inspect
finally, there is a "pp" module to pretty-print variable (pretty much like the pprint module in python):
require "pp"
pp dictionary
Hope it helps !

Try to convert object to JSON
dictionary.to_json

You could also do p dictionary which sends inspect by default:
dictionary = {
"test" => 2,
"try" => 2
}
p dictionary # => {"test"=>2, "try"=>2}

Related

ruby object to_s gives unexpected output

What is the correct way to view the output of the puts statements below? My apologies for such a simple question.... Im a little rusty on ruby. github repo
require 'active_support'
require 'active_support/core_ext'
require 'indicators'
my_data = Indicators::Data.new(Securities::Stock.new(:symbol => 'AAPL', :start_date => '2012-08-25', :end_date => '2012-08-30').output)
puts my_data.to_s #expected to see Open,High,Low,Close for AAPL
temp=my_data.calc(:type => :sma, :params => 3)
puts temp.to_s #expected to see an RSI value for each data point from the data above
Maybe check out the awesome_print gem.
It provides the .ai method which can be called on anything.
An example:
my_obj = { a: "b" }
my_obj_as_string = my_obj.ai
puts my_obj_as_string
# ... this will print
# {
# :a => "b"
# }
# except the result is colored.
You can shorten all this into a single step with ap(my_obj).
There's also a way to return objects as HTML. It's the my_obj.ai(html: true) option.
Just use .inspect method instead of .to_s if you want to see internal properties of objects.

Simple JSON not working in Ruby

Code:
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'json'
def getData
file = open("http://goo.gl/BI6h7a")
#json = JSON.parse(file.read)
end
getData
cveIds = #json['cve_id']
puts cveIds
You can see the JSON response here: http://goo.gl/BI6h7a
Console:
./cve.rb:13:in `[]': can't convert String into Integer (TypeError) from ./cve.rb:13:in `<main>'
I don't know why this is happening. "Convert String into Integer"? WHAT?
The #json gets the content fine, but the cveIds doesn't.
The top element in the json that you're reading is actually an Array, each of its elements is actually a hash, it's like this:
[
{
"cve_id": "CVE-2014-3976"
// other key/value pairs
}
{
"cve_id": "CVE-2014-3975"
// other key/value pairs
}
{
"cve_id": "CVE-2014-3974"
// other key/value pairs
}
// .... more hashes
]
so #json is an array. And if you want to access any of its elements you have to access it with a numeric integer index like, so:
#json[0] # => { "cve_id": "CVE-2014-3976", // other key/value pairs }
I think you are trying to collect the cve_id fields of all these hashes, this can be done as follows:
cveIds = #json.collect { |h| h["cve_id"] }
# The result:
=> ["CVE-2014-3976", "CVE-2014-3975", "CVE-2014-3974", "CVE-2014-3962", "CVE-2014-3961",
"CVE-2014-3878", "CVE-2014-3871", "CVE-2014-3842", "CVE-2014-3806", "CVE-2014-3792",
"CVE-2014-3791", "CVE-2014-3443", "CVE-2014-3247", "CVE-2014-3246", "CVE-2014-3225",
"CVE-2014-3216", "CVE-2014-3139", "CVE-2014-3138", "CVE-2014-3008", "CVE-2014-2996",
"CVE-2014-2994", "CVE-2014-2976", "CVE-2014-2850", "CVE-2014-2847", "CVE-2014-2671",
"CVE-2014-2668", "CVE-2014-2588", "CVE-2014-2587","CVE-2014-2586", "CVE-2014-2579"]
I'm not a ruby developer but what you have there is a list if dictionaries.
My guess in order for you to read cve_id you need to create some kind of a for loop.
for example in python I would write it like this:
for line in my_data:
print line['cve_id']
I guess in ruby it would look like this:
for i in #json do
cveIds = i['cve_id']
puts cveIds
end
cveIds = #json['cve_id']
What are you doing here is equivalent to:
arr = [1, 2, 3, 4]
puts arr["hello"] # using a string here on an indexed based array!
Hence your error message about Ruby trying to convert a String to an int.
Try the following instead
cveIds = #json.first['cve_id'] # equivalent to #json[0]['cve_id']
puts cveIds
In the above code sample, we are getting the first element from the array, which is a hash we can then access cve_id from.

MongoDB + Ruby. How to access document properties?

I want to try Mongo with Ruby. I connected, selected collection and I can query data from MongoDB.
irb(main):049:0> coll.find_one({:x=>4})
=> #<BSON::OrderedHash:0x3fdb33fdd59c {"_id"=>BSON::ObjectId('4f8ae4d7c0111ba6383cbe1b'), "x"=>4.0, "j"=>1.0}>
irb(main):048:0> coll.find_one({:x=>4}).to_a
=> [["_id", BSON::ObjectId('4f8ae4d7c0111ba6383cbe1b')], ["x", 4.0], ["j", 1.0]]
But how to access propeties, when I retrieve BSON hash? I need something like this:
data.x
=> 4
to_hash method gives me the same BSON::OrderedHash... :(
When you say coll.find_one({:x=>4}), you get a BSON::OrderedHash back that you access like a normal Hash:
h = coll.find_one(:x => 4)
puts h['x']
# 4 comes out unless you didn't find anything.
If you use a full find instead of find_one, you get a MongoDB::Cursor which is an Enumerable so you can iterate it like any other collection; the cursor will return BSON::OrderedHash instances as you iterate so you can do things like this:
cursor = coll.find(:thing => /stuff/)
cursor.each { |h| puts h['thing'] }
things = cursor.map { |h| h['thing'] }
If you wanted objects instead of Hashes then you'd have to wrap the MongoDB::Cursor and BSON::OrderedHash instances with object yourself (possibly via Struct).
Mongodb find_one method returns hash object, find method returns cursor object.
Cursor object can be iterated and then is possible to extract the answer in a normal hash.
require 'rubygems'
require 'mongo'
include Mongo
client = MongoClient.new('localhost', 27017)
db = client.db("mydb")
coll = db.collection("testCollection")
coll.insert({"name"=>"John","lastname"=>"Smith","phone"=>"12345678"})
coll.insert({"name"=>"Jane","lastname"=>"Fonda","phone"=>"87654321"})
cursor = coll.find({"phone"=>"87654321"})
answer = {}
cursor.map { |h| answer = h }
puts answer["name"]

Parsing a .cfg file using Ruby?

I have a .cfg file with the following data:
*.*.key_val = {
key1= "value1";
key2 = "value2";
key3 = "value3";
};
I want to read this file and store the key value pairs in a hash #var[key][val].
How it can be done?
THIS cfg you may parse in such way:
read file using File#read
convert text into 2-dimentional array using String#scan and regex
convert array into hash using Hash[]
text = File.read('your.cfg')
# => "*.*.key_val = {\n key1= \"value1\";\n key2 = \"value2\";\n key3 = \"value3\";\n};"
data = text.scan(/(\S+)\s*=\s*"([^"]+)/)
# => [["key1", "value1"], ["key2", "value2"], ["key3", "value3"]]
#var = Hash[data]
# => {"key1"=>"value1", "key2"=>"value2", "key3"=>"value3"}
Or just:
#var = Hash[File.read('your.cfg').scan(/(\S+)\s*=\s*"([^"]+)/)]
I'd strongly recommend transferring the configuration to something like YAML. It's made to be easy to understand, flexible, universally implemented, well documented, both as a standard and as a part of the core library, and easy to understand. (Yes, I said it twice on purpose.)
My YAML files load into Ruby as a Hash when I do something like:
require 'yaml'
config = YAML.load_file('/path/to/config/file')
I'll create the initial template for the configuration file in Ruby, as a Hash, then serialize it and write it to disk. That way I know what's on the disk is exactly the way YAML wants it to be and helps me avoid that "it won't load because either the data is wrong or the code is wrong" quandary.
# A simple round-trip (load and dump) of an object.
require 'yaml'
test_obj = {
'foo' => 'bar',
'one_two_three' => [1, 2, 3],
'hash' => {'another' => 'hash'}
} #=> {"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
File.open('./config.yaml', 'w') { |fo| fo.puts YAML::dump( test_obj ) } #=> nil
ruby_obj = YAML::load_file( './config.yaml' ) #=> {"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
ruby_obj == test_obj #=> true
require 'pp'
pp ruby_obj
{"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
pp test_obj
{"foo"=>"bar", "one_two_three"=>[1, 2, 3], "hash"=>{"another"=>"hash"}}
You should try out the 'parseconfig' gem: https://rubygems.org/gems/parseconfig/
gem install parseconfig
Here's a sample how to use this gem:
require 'rubygems'
require 'parseconfig'
my_config = ParseConfig.new('your_file.cfg')
puts my_config.get_value('key_val')
Good luck and have fun learning Ruby. :)
EDIT
As Glenux said this is only for simple configuration files. I'll check if I can find anything else.
EDIT 2
I can't find a gem or something to parse a cfg file like in your example. I guess your only option is to write a parser yourself (like Nakilon did) or use something like YAML instead. Good luck anyway. :)
The parseconfig class is only intended for simple configuration files !
It accepts files of the format "param = value" (cf http://www.5dollarwhitebox.org/drupal/projects#rb-parseconfig ) but it will not parse the *.*.key_val = { and } thing.
Is the configuration file yours or generated/used by a third-party software? If it is yours, it may be wiser to use an other configuration file format (JSON, Ini, YAML, etc).
Wanted to mention this ruby library that can help you transfer configuration between JSON, YAML or Windows Ini file formats:
https://github.com/kigster/dupervisor
The use case is to move the configuration to YAML, but be able to generate whatever the format is needed by the software – in the case of this gem – it's supervisord's INI file format.

how to store a Ruby array into a file?

How to store a Ruby array into a file?
I am not sure what exactly you want, but, to serialize an array, write it to a file and read back, you can use this:
fruits = %w{mango banana apple guava}
=> ["mango", "banana", "apple", "guava"]
serialized_array = Marshal.dump(fruits)
=> "\004\b[\t\"\nmango\"\vbanana\"\napple\"\nguava"
File.open('/tmp/fruits_file.txt', 'w') {|f| f.write(serialized_array) }
=> 33
# read the file back
fruits = Marshal.load File.read('/tmp/fruits_file.txt')
=> ["mango", "banana", "apple", "guava"]
There are other alternatives you can explore, like json and YAML.
To just dump the array to a file in the standard [a,b,c] format:
require 'pp'
$stdout = File.open('path/to/file.txt', 'w')
pp myArray
That might not be so helpful, perhaps you might want to read it back? In that case you could use json. Install using rubygems with gem install json.
require 'rubygems'
require 'json'
$stdout = File.open('path/to/file.txt', 'w')
puts myArray.to_json
Read it back:
require 'rubygems'
require 'json'
buffer = File.open('path/to/file.txt', 'r').read
myArray = JSON.parse(buffer)
There are multiple ways to dump an array to disk. You need to decide if you want to serialize in a binary format or in a text format.
For binary serialization you can look at Marshal
For text format you can use json, yaml, xml (with rexml, builder, ... ) , ...
Some standard options for serializing data in Ruby:
Marshal
YAML
JSON (built-in as of 1.9, various gems available as well)
(There are other, arguably better/faster implementations of YAML and JSON, but I'm linking to built-ins for a start.)
In practice, I seem to see YAML most often, but that may not be indicative of anything real.
Here's a quick yaml example
config = {"rank" => "Admiral", "name"=>"Akbar",
"wallet_value" => 9, "bills" => [5,1,1,2]}
open('store.yml', 'w') {|f| YAML.dump(config, f)}
loaded = open('store.yml') {|f| YAML.load(f) }
p loaded
# => {"name"=>"Akbar", "wallet_value"=>9, \
# "bills"=>[5, 1, 1, 2], "rank"=>"Admiral"}
Example: write text_area to a file where text_area is an array of strings.
File.open('output.txt', 'w') { |f| text_area.each { |line| f << line } }
Don't forget to do error checking on file operations :)
Afaik.. files contain lines not arrays. When you read the files, the data can then be stored in an array or other data structures. I am anxious to know if there is another way.

Resources