How to check N is a power of 2 ?
def is_a_power_of_2?(n)
puts "#{n} is a power of 2!" if n.bit_length != (n-1).bit_length
endHow to check N is a power of 2 ?
def is_a_power_of_2?(n)
puts "#{n} is a power of 2!" if n.bit_length != (n-1).bit_length
end| # SPOJ - PRIME1 http://www.spoj.com/problems/PRIME1/ | |
| # | |
| # *NOTE* | |
| # | |
| # 1. Presieve the primes under sqrt(1_000_000_000). | |
| # 2. Multiple the primes to flag numbers which are not primes. | |
| # 3. Sieve in given range. | |
| primes = [2] |
| require 'digest/sha1' | |
| require 'zlib' | |
| require 'fileutils' | |
| # put_raw_object("what is up, doc?", 'blob') | |
| def put_raw_object(content, type) | |
| # Generate SHA-1 | |
| header = "#{type} #{content.length}\0" # => "blob 16\000" | |
| store = header + content # => "blob 16\000what is up, doc?" | |
| sha1 = Digest::SHA1.hexdigest(store) # => "bd9dbf5aae1a3862dd1526723246b20206e5fc37" |
| File.open("stats/stats_#{Time.now.to_i}.tsv","w") do |of| | |
| lines = `git log --format=format:"%an\t%ae\t%ce"`.split("\n").uniq | |
| stats = lines.reduce({}) do |ret, line| | |
| author, a_email, c_email = line.strip.split("\t") | |
| ret[author] ||= {} | |
| ret[author][:a_email] ||= [] | |
| ret[author][:c_email] ||= [] | |
| ret[author][:a_email] << a_email unless ret[author][:a_email].include? a_email | |
| ret[author][:c_email] << c_email unless ret[author][:c_email].include? c_email | |
| ret |
Now $git show-branch master server client shows like this:
<-- commit0 <-- commit1 <-- commit2 master
\
\-- commit3 <-- commit4 server
\
\-- commit5 <-- commit6 client| [34] pry(main)> edit -h | |
| Usage: edit [--no-reload|--reload|--patch] [--line LINE] [--temp|--ex|FILE[:LINE]|OBJECT|--in N] | |
| Open a text editor. When no FILE is given, edits the pry input buffer. | |
| When a method/module/command is given, the code is opened in an editor. | |
| Ensure `Pry.config.editor` is set to your editor of choice. | |
| edit sample.rb edit -p MyClass#my_method | |
| edit sample.rb --line 105 edit MyClass | |
| edit MyClass#my_method edit --ex |
Write a file named two-threads.rb.
t1 = Thread.new{ while true; end }
t2 = Thread.new{ while true; end }
t1.join
t2.join| class Foo | |
| def i_method | |
| @foo = '' | |
| end | |
| @bar = '' | |
| class << self | |
| @baz = '' |
| require 'benchmark' | |
| require 'faraday' | |
| require 'faraday_middleware' | |
| class Wendi | |
| def initialize(app) | |
| @app = app | |
| end | |
| def call(env) | |
| puts 'wendi requesting...' |
| require 'rubygems' | |
| require 'madeleine' | |
| class Employee | |
| attr_accessor :name, :number, :address | |
| def initialize(name, number, address) | |
| @name = name | |
| @number = number | |
| @address = address |