Skip to content

Instantly share code, notes, and snippets.

@launchkit-codes
launchkit-codes / date.rb
Last active February 25, 2016 15:35
strftime and %F
Time.now.strftime("%F %T") # => shortcut to strftime('%Y-%m-%d HH:MM:SS')
@launchkit-codes
launchkit-codes / splat_operator.rb
Last active February 25, 2018 19:10
Splat Operator and Arrays
cell_42 = ['inmate #42', 'inmate #43']
common_room = ['inmate #40', 'inmate #41', *cell_42, 'inmate #44']
# => ['inmate #40', 'inmate #41', 'inmate #42', 'inmate #43', 'inmate #44']
@launchkit-codes
launchkit-codes / beverage_selector.rb
Last active August 29, 2015 14:21
Pick up the right infos from a criteria
# Use this pattern instead of a long case when:
# case cocktail
# when 'mojito'
# ["lemon", "mint", "rhum"]
# ...
# when 'lemon coke'
# ["coke", "lemon"]
# end
@launchkit-codes
launchkit-codes / alias_on_class_method.md
Last active March 27, 2018 07:11
Alias on Class Method

Class methods are methods added on the singleton class of the object.

class Mail
  def self.is_delivered?
    true
  end
  
  self.singleton_class.send(:alias_method, :delivered?, :is_delivered?)
end
@launchkit-codes
launchkit-codes / ar_scoping.rb
Last active August 29, 2015 14:23
ActiveRecord Scoping
# AR Scoping permit to dinamically build you query in multiple steps
# Initialize an 'empty' scope
scope = Pokemon.where(nil)
scope = scope.select(:id)
scope = scope.where(created_at: (Time.now - 1.month)..(Time.now))
scope = scope.limit(5)
@launchkit-codes
launchkit-codes / index.html
Created September 18, 2015 14:21 — forked from anonymous/index.html
JS Bin react - state // source https://jsbin.com/boqulusofu
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="react - state">
<script src="http://fb.me/react-0.13.1.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
#spacer{
margin: 100px 0 20px 0;
@launchkit-codes
launchkit-codes / bitwise.rb
Last active November 16, 2015 22:35
Demystify The Bitwise Operators
8.to_s(2) # => "00001000"
6.to_s(2) # => "00000110"
2.to_s(2) # => "00000010"
1.to_s(2) # => "00000001"
(2 & 1).to_s(2) # => "00000000" because it applies an AND operation on each bit. (1 & 1) match
(2 | 1).to_s(2) # => "00000011" because it applies an OR operation on each bit. (1 | 1), (1 | 0), (0 | 1) match !
(6 ^ 2).to_s(2) # => "00000100" because it applies a XOR operation on each bit. (1 | 0), (0 | 1) match !
(8 >> 2).to_s(2) # => "00000010" right-shift operator. Each bit is shifted 2 bits to the right.
(2 << 2).to_s(2) # => "00001000" left-shift operator. Each bit is shifted 2 bits to the left.
@launchkit-codes
launchkit-codes / LINEOFCODE.md
Last active August 14, 2016 16:27
Count the lines of code in a Gem

###Prerequisites

?> gem install wcr

###Then

?&gt; wcr -l `ls | grep -v -E "LICENSE|Gemfile\.lock"` # chain files that you want to omit.
@launchkit-codes
launchkit-codes / convert.md
Last active August 14, 2016 16:26
Convert a file from ISO-8859-1 to UTF-8
iconv -f ISO-8859-1 -t UTF-8 your_file.txt > converted_file.txt

###Contact

Thanks for reading. I'm available for freelance work !

Twitter
GitHub

@launchkit-codes
launchkit-codes / case.md
Last active May 9, 2017 06:38
Case clause Behind The Scene

Case statement Behind The Scene

Syntax

A case statement consists of an optional condition followed by zero or more when conditions. It returns the value of the first truthy when statement. Otherwise nil.

str = case "match"