Skip to content

Instantly share code, notes, and snippets.

View mindaslab's full-sized avatar

Karthikeyan A K mindaslab

View GitHub Profile
@mindaslab
mindaslab / parallel_assignment.bash
Created November 11, 2013 04:38
Parallel assignment in ruby
Despite being a rubyist for years, I was fooled by this
irb> a = b = c = []
irb> a << 1
=> [1]
irb> b
=> [1]
irb> c
=> [1]
@mindaslab
mindaslab / saving_without_timestamp.bash
Created November 12, 2013 07:56
Saving a record without timestamps
2.0.0-p247 :002 > t = Tweet.new
=> #<Tweet id: nil, content: nil>
2.0.0-p247 :003 > t.content = "Tweet without time"
=> "Tweet without time"
2.0.0-p247 :004 > t.save
(0.3ms) begin transaction
SQL (0.9ms) INSERT INTO "tweets" ("content") VALUES (?) [["content", "Tweet without time"]]
(190.3ms) commit transaction
=> true
2.0.0-p247 :005 > Tweet.all
@mindaslab
mindaslab / 20131112072136_create_tweets.rb
Created November 12, 2013 07:58
Rails table creation without time stamp
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.text :content
end
end
end
@mindaslab
mindaslab / searchable.rb
Created June 11, 2014 05:33
Code for searching Active Rcords
module Searchable
extend ActiveSupport::Concern
included do
##
# A simple search method
def self.search text
columns = self::SearchableColumns
words = text.downcase.gsub(/[^a-z0-9\s]/i, ' ').split(/\s+/)
query_array_2 = []
console.log("Hello World!\n");
function sort_by_price(a, b){
return a.price > b.price;
}
array = [{price: 100}, {price: 50}, {price: 70}];
console.log(array);
sorted_array = array.sort(sort_by_price);
@mindaslab
mindaslab / prime.rb
Last active August 29, 2015 14:19
Primes in Ruby
class Fixnum
def prime?
return true if self == 1
2.upto(self) do |num|
if self % num == 0
return self == num
end
end
end
end
def prime(num):
if num == 1:
return True
for i in range(num):
if (num % (i + 2)) == 0:
if num == (i + 2):
return True
else:
return False
class Fixnum
def prime?
return true if self == 1
2.upto(self) do |num|
if self % num == 0
return self == num
1.upto(100) do |num|
puts num if num.prime?
@mindaslab
mindaslab / py_ftp.sh
Created May 13, 2015 03:10
Python FTP server
$ pip install pyftpdlib
$ python -m pyftpdlib
# Temporary FTP server
class User < ActiveRecord::Base
def status time = Time.now
statuses.where("created_at <= '#{time.to_s(:db)}'").
order("created_at desc").first
end
end