Skip to content

Instantly share code, notes, and snippets.

View AhmedNadar's full-sized avatar
🌎
Ready to work remote with you 😀 💻 🤝 👏🏼

Ahmed Nadar AhmedNadar

🌎
Ready to work remote with you 😀 💻 🤝 👏🏼
View GitHub Profile
@AhmedNadar
AhmedNadar / tinyurl.rb
Created May 18, 2012 22:18 — forked from woods/tinyurl.rb
Ruby: A complete URL-shortening web application in Sinatra.
#!/usr/bin/env ruby
#
# A complete URL-shortening web application, written in Ruby/Sinatra. Run it
# from the command line, and then visit http://localhost:4567/
#
# Or to run it under apache/passenger, you'll need a config.ru file with the
# following contents:
#
# require 'tinyurl'
# run Sinatra::Application
@AhmedNadar
AhmedNadar / gist:6409369
Last active December 22, 2015 03:18 — forked from padolsey/gist:527683
JavaScript: Detect IS
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
@AhmedNadar
AhmedNadar / gist:6409477
Last active December 22, 2015 03:19
JavaScript: jQuery Store all favorites Twitter in instapaper
$ = window.jQuery;
var html = '' +
' <form action="//www.instapaper.com/bookmarklet/post_v5" method="post" id="instapaper"> \
<input type="hidden" name="p" value=""/> \
<input type="hidden" name="b" id="b" value=""/> \
</form>';
@AhmedNadar
AhmedNadar / gist:6409508
Last active December 22, 2015 03:19
CSS: Image replacment
// A new way to use an image replacment without using text indent to form a box way off the screen
ir{
border: 0;
font: 0/0;
text-shadow: none;
color: transparent;
background-color: transparent;
}
@AhmedNadar
AhmedNadar / gist:6409547
Last active December 22, 2015 03:19
JavaScript: jQuery Pubsub
(function($){
var o = $( {} );
$.each({
on: 'subscribe',
trigger: 'publish',
off: 'unsubscribe'
}, function(key, api) {
$[api] = fiunction(){
o[key].apply(o, arguments);
@AhmedNadar
AhmedNadar / photo_essay.rb
Created September 2, 2013 05:53 — forked from levity/photo_essay.rb
Ruby: photo assay
#!/usr/bin/ruby
require 'yaml'
# DATA is a little-used feature of the Ruby language; it's a file handle
# whose contents are everything in the current file after the __END__.
# YAML is "Yet Another Markup Language".
data = YAML.load(DATA)
def url_for(entry_num)
@AhmedNadar
AhmedNadar / install_vagrant
Created October 18, 2013 20:29
Install Vagrant. This is history of my terminal while trying to get Vagrant install.
➜ pairing git:(master) vagrant up
Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires
this to be available on the PATH. If VirtualBox is installed, please find the
`VBoxManage` binary and add it to the PATH environmental variable.
➜ pairing git:(master) vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Box 'raring-minimal' was not found. Fetching box from specified URL for
the provider 'virtualbox'. Note that if the URL does not have
a box for this provider, you should interrupt Vagrant now and add
@AhmedNadar
AhmedNadar / max_2_sum
Last active December 27, 2015 02:39
Calculate the sum of largest 2 element of an array.
#Calculate the sum of largest 2 elements of an array.
def max_2_sum(arr)
arr.inject([0, 0]) do |largest, el|
if largest[0] < el
largest[1] = largest[0]
largest[0] = el
elsif largest[1] < el
largest[1] = el
end
@AhmedNadar
AhmedNadar / sum_array
Created October 31, 2013 18:24
Calculate the sum of all elements in an array
# Calculate the sum of all elements in an array
def sum(array)
array.inject{|sum, e| sum + e}
end
# or i can use shortcut
def sum(array)
array.inject(:+)
end
@AhmedNadar
AhmedNadar / gist:7256203
Created October 31, 2013 20:08
Define a method sum_to_n? which takes an array of integers and an additional integer, n, as arguments and returns true if any two elements in the array of integers sum to n. An empty array should sum to zero by definition.
def sum_to_n?(arr, n)
(arr.empty? && n.zero?) || arr.permutation(2).any? { |a, b| a + b == n }
end
# OR
def sum_to_n?(arr, n)
return true if arr.empty? && n.zero?
arr.combination(2).any? {|a, b| a + b == n }
end