Listing posts
Displaying posts 271 - 275 of 328 in total2017-08-17
Here is some empirical data from my notebook, the last command is the fastest:
1 2 3 4 5 6 7 8 9 10 | # ~40MB/s | OpenSSL rand openssl rand 1000000000 | pv > /dev/null # ~175MB/s | /dev/urandom pv < /dev/urandom > /dev/null # ~1200MB/s | OpenSSL enc /dev/zero openssl enc -aes-256-ctr -nosalt \ -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" \ < /dev/zero | pv > /dev/null |
Source: OpenSSL rand, OpenSSL enc /dev/zero
~~~ * ~~~
2017-07-31
In case of a SystemStackError: stack level too deep
exception you do not have a backtrace to inspect, but set_trace_func
comes in help!
- put this in an initializer:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # /config/initializers/ruby_tracing.rb if ENV['RUBY_TRACING'] $rb_tracing_enable = false # set to true just before the desired method $rb_tracing_file = File.open(Rails.root.join('rb_tracing.log'), 'w') $rb_tracing_file.sync = true set_trace_func(proc{|event, file, line, id, binding, classname| if $rb_tracing_enable && event == 'call' $rb_tracing_file.puts "#{file}:#{line} #{classname}##{id}" end }) end |
- start the webserver with the
RUBY_TRACING
env var initialized:
1 | RUBY_TRACING=true rails s |
- set the
$rb_tracing_enable
flag var near the code that explodes:
1 2 | $rb_tracing_enable = true a_method_that_causes_infinite_recursion_in_a_not_obvious_way() |
- inspect the
/rb_tracing.log
file
1 | grep project_path rb_tracing.log |
Source: stackoverflow, jbgo's gist
~~~ * ~~~
2017-07-31
GZIP/GUNZIP a string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class String # http://ruby-doc.org/stdlib-2.2.0/libdoc/zlib/rdoc/Zlib/GzipWriter.html # https://gist.github.com/sinisterchipmunk/1335041 def gzipped(fname = 'file.txt') gz_file = StringIO.new z = Zlib::GzipWriter.new(gz_file) z.mtime = Time.now # for gzip compat z.orig_name = fname # for gzip compat z.write self z.close gz_file.string end # gzipped def gunzipped gz_file = StringIO.new self z = Zlib::GzipReader.new(gz_file) z.read end # gunzipped end |
Create a ZIP archive on the fly:
1 2 3 4 5 6 7 | require 'zip' # https://github.com/rubyzip/rubyzip # Zip.default_compression = Zlib::BEST_COMPRESSION Zip::File.open("archive.zip", Zip::File::CREATE) do |zipfile| zipfile.get_output_stream("hello.txt"){|f| f.write "hello world" } end |
~~~ * ~~~
2017-07-29
Given this sample class with two relations on the same table:
1 2 3 4 | def Edge < ActiveRecord::Base belongs_to :first , class_name: 'Node' belongs_to :second, class_name: 'Node' end |
If you want to query edges by the first node, you could do this:
1 | Edge.joins(:first).where(nodes: {value: 1}) |
1 2 3 | SELECT "edges".* FROM "edges" INNER JOIN "nodes" ON "nodes"."id" = "edges"."first_id" WHERE "nodes"."value" = 1 |
But if you have to query using both nodes, you can still use joins like this:
1 | Edge.joins(:first, :second).where(nodes: {value: 1}, seconds_edges: {value: 2}) |
1 2 3 4 5 6 | SELECT "edges".* FROM "edges" INNER JOIN "nodes" ON "nodes"."id" = "edges"."first_id" INNER JOIN "nodes" "seconds_edges" ON "seconds_edges"."id" = "edges"."second_id" WHERE "nodes"."value" = 1 AND "seconds_edges"."value" = 2 |
Source: Stackoverflow
~~~ * ~~~
2017-07-27
preload
Loads data in a separate query (we cannot place conditions on the association):
1 | User.preload(:posts) |
1 2 | SELECT users.* FROM users SELECT posts.* FROM posts WHERE posts.user_id IN (1) |
eager_load
Loads data in a single LEFT OUTER JOIN
query (we can place conditions on the association):
1 | User.eager_load(:posts) |
1 2 3 | SELECT users.id AS t0_r0, users.name AS t0_r1 , users...., posts.id AS t1_r0, posts.user_id AS t1_r1, posts.... FROM users LEFT OUTER JOIN posts ON posts.user_id = users.id |
includes
Loads data in a separate or single query based on conditions:
1 2 | User.includes(:posts) User.includes(:posts).where(posts: {title: 'hello'}) |
1 2 3 4 5 6 7 8 9 | -- 1st case - without conditions SELECT users.* FROM users SELECT posts.* FROM posts WHERE posts.user_id IN (1) -- 2nd case - with conditions SELECT users.id AS t0_r0, users.name AS t0_r1 , users...., posts.id AS t1_r0, posts.user_id AS t1_r1, posts.... FROM users LEFT OUTER JOIN posts ON posts.user_id = users.id WHERE posts.title = 'hello' |
join
Does not load any data, just uses an INNER JOIN
query:
1 | User.joins(:posts) |
1 | SELECT users.* FROM users INNER JOIN posts ON posts.user_id = users.id |
Source: BigBinary