Tag Archives: ruby

Ruby: array to hash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env ruby
require 'awesome_print'

url = %x[curl -s -I ya.ru |egrep -v HTTP]
options = []
url.split("\r\n").each do |el|
options << el.split(": ")
end

options = options.to_h

puts "Whole Hash-array: "
ap options

puts "Option by key 'Server' "
ap options["Server"]

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Whole Hash-array:
{
"Server" => "nginx",
"Date" => "Mon, 19 May 2014 17:47:10 GMT",
"Content-Type" => "text/html; charset=UTF-8",
"Content-Length" => "8159",
"Connection" => "close",
"Cache-Control" => "no-cache,no-store,max-age=0,must-revalidate",
"Expires" => "Mon, 19 May 2014 17:47:10 GMT",
"Last-Modified" => "Mon, 19 May 2014 17:47:10 GMT",
"P3P" => "policyref=\"/w3c/p3p.xml\", CP=\"NON DSP ADM DEV PSD IVDo OUR IND STP PHY PRE NAV UNI\"",
"Set-Cookie" => "yandexuid=8305933271400521630; Expires=Thu, 16-May-2024 17:47:10 GMT; Domain=.ya.ru; Path=/",
"X-Frame-Options" => "DENY",
"X-XRDS-Location" => "http://openid.yandex.ru/server_xrds/"
}
Option by key 'Server'
"nginx"

Rails: model conception

You should create new model for each table:
rails generate model Tank

it'll create migration Tanks, you should open db/migrate/${date}_create_tanks.rb

and put there definition of table

After you should open model  app/models/tank.rb

and put there tables relations.

After creating models you should use RAILS_ENV=development rake db:migrate for creating database with tables and relations between the last one

Rails: dump db schema from mysql

gem install mysql2

edit config/database.yml

development:
    adapter: mysql2
    encoding: utf8
    database: db_name
    username: user_name
    password: password_of_user
    host: host_ip
    timeout: 5000

edit Gemfile

gem 'mysql2'

bundle install

rake db:schema:dump

db/schema.rb has been created