Skip to content

Instantly share code, notes, and snippets.

View aruprakshit's full-sized avatar
🏠
Working from home

Arup Rakshit aruprakshit

🏠
Working from home
View GitHub Profile
class Object
def send_through(object, *args)
object.dispatcher_for(self).call(self, *args)
end
end
module Dispatcher
class DispatcherNotFound < StandardError; end
def self.extended(klass)
"ab"
"a
b"
def store(provider, board, status)
# @connection is a PG::Connection object
# @logger is a Logger object
# The part before `.each` is inconsequential, it's just a prepared statement and we're
# iterating over the results.
@connection.exec_prepared(TYPE, [provider, board].concat(statement status)).each do |row|
# Ignore this
@logger.debug(Harvester::Runner) do
Scrawl.new(label: "insert", table: TABLE, id: row["id"])
@aruprakshit
aruprakshit / count_delete_char.rb
Created July 9, 2014 10:40
count of deleted charcaters
class Array
def number_of_items_deleted(char)
self.size - self.tap { |o| o.delete(char) }.size
end
end
puts %w(a b a d t r).number_of_items_deleted 'a'
# >> 2

Originally published in June 2008

When hiring Ruby on Rails programmers, knowing the right questions to ask during an interview was a real challenge for me at first. In 30 minutes or less, it's difficult to get a solid read on a candidate's skill set without looking at code they've previously written. And in the corporate/enterprise world, I often don't have access to their previous work.

To ensure we hired competent ruby developers at my last job, I created a list of 15 ruby questions -- a ruby measuring stick if you will -- to select the cream of the crop that walked through our doors.

What to expect

Candidates will typically give you a range of responses based on their experience and personality. So it's up to you to decide the correctness of their answer.

require 'socket'
require 'fiber'
#A list of readables and writables for IO.select, and which fiber is waiting for which IO
watch = {
readable: {}, #{io => fiber}
writable: {}
}
@aruprakshit
aruprakshit / Enumrable.md
Created July 7, 2014 04:05
Enumerable vs Enumerator

Enumerable is a module, and Enumerator is a class. So that should tell you a lot out the the box. Enumerable is a set of behaviors and functionality you'd want to add to a class. The Enumerator class is something you get objects from itself.

class Wrong
def method_missing(m, *)
if m =~ /\Ahello_(.+)\z/
puts "Hello, #{$1.capitalize}"
else
super
end
end
end
@aruprakshit
aruprakshit / to_a.md
Last active August 29, 2015 14:03
Playing with Enumerable#to_a

Enumerable#to_a says, that it can take arguments also. Lets try

(1..4).to_a(1,2)
#ArgumentError: wrong number of arguments (2 for 0)

Oops! Why then error ? Because Enumerable#to_a called actually Range#each which don't accept any arguments. Now look the below code :-

@aruprakshit
aruprakshit / respond_to.rb
Last active August 29, 2015 14:03
respond_to? vs respond_to_missing?
# First check is always #respond_to? , if it returns false, then #respond_to_missing? will be called, otherwise #respond_to_missing?
# will never be called. For all regular methods( which has been created by def, define_method, in C-implementation etc), thus we get
# false, while we pass the method name to the #respond_to_missing?. Because, first check has been done inside #respond_to?, and it
# returns true. That's why second output is [true, false]. Because, #foo is a regular method.
# Now why the second output is [true, true] ? The reason, I already explained, again #baz is not a regular method, so first check in
# #respond_to? return false, thus check goes to #respond_to_missing?. As I implemented it, so as per the implementations it is giving
# true, which in turn caused #respond_to? method to returns to true.