Skip to content

Instantly share code, notes, and snippets.

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

Kushal mistrikushal

🏠
Working from home
View GitHub Profile
@mistrikushal
mistrikushal / Rspec private methods.txt
Last active December 8, 2015 11:08
RSpec - Testing private methods of a model
# RSpec allows us to test private methods of a model.
for example,
class User
def is_active?
true
end
private :is_active?
end
@mistrikushal
mistrikushal / gist:e4a678141eca354a79db
Created December 9, 2015 10:26
mysql gem installation issue
if you see an error while installing mysql gem like,
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
solution:
sudo apt-get install mysql-server
sudo apt-get install mysql-client libmysqlclient-dev
sudo apt-get install libmysqld-dev libmysqlclient-dev mysql-client
to create a .zip of a directory using bash shell,
$ zip -r filename.zip directory_path_to_zip
@mistrikushal
mistrikushal / ruby_puzzle_1.rb
Created January 11, 2016 11:50
a small ruby puzzle solution
# check first non-repeating element from array
arr = [1,200,1,200,3,4,5]
res = Array.new
arr.detect{|e| res << e unless(arr.count(e) > 1) }
puts "First non-repeated element in Array"+arr.to_s+" is : "+ res.first.to_s
# check even times repeating elements in array
a = [5, 3, 5, 1, 5, 1, 3]
res_arr = Array.new
@mistrikushal
mistrikushal / longest_word_test.rb
Created January 26, 2016 13:52
find a longest word from a given string
# logic to check longest word from a string
str = 'this string contains a looooooong word'
longest_word = str.split(' ').max_by(&:length)
puts "Longest word of the string is: #{longest_word}"
@mistrikushal
mistrikushal / test_multiply.rb
Last active January 26, 2016 14:43
multiply 2 numbers without X operator
# perform multiplication on 'a' and 'b' without using X operator
def mul(a, b)
x = 0
b.times do
x = x + a
end
puts "Multiplication of #{a.to_s} X #{b.to_s} = #{x.to_s}"
end
Verifying that "kushalmistri.id" is my Blockstack ID. https://onename.com/kushalmistri
@mistrikushal
mistrikushal / time_difference_test.rb
Created September 8, 2016 10:29
Time difference test
require 'time'
require 'rubygems'
def calc_time_diff(current_time, end_time, end_time2=nil)
parsed_end_time = Time.parse(end_time)
parsed_current_time = Time.parse(current_time)
result = Hash.new
if parsed_end_time < parsed_current_time
secs_diff = (parsed_current_time - parsed_end_time).to_i.abs
@mistrikushal
mistrikushal / csv_generate.rb
Last active September 22, 2016 05:45
create csv in ruby on rails
# Create a CSV in Write Mode
CSV.open("#{Rails.public_path}/file_name.csv", 'wb') do |csv|
# Add a first row into CSV which is column names
csv << ['First name', 'Last name', 'Email']
end
# Now we have CSV file created in the public directory of the rails application and also it has columns ready
# Now Let's add some data into it. please note that the data in each cell of the row should be in order with the columns defined.
@mistrikushal
mistrikushal / prac.rb
Created January 10, 2017 10:42
practical test
COMMANDS = ['DEPEND', 'INSTALL', 'REMOVE', 'LIST']
@component_dependencies = Hash.new
@installed_components = Array.new
def validate_command(command)
command_size = command.size
command_name = command.split(' ').first
return false unless COMMANDS.include?(command_name)
return false if command_name != command_name.upcase