Skip to content

Instantly share code, notes, and snippets.

@moreati
moreati / example.md
Last active May 19, 2018 06:20
Read and Execute permissions on Linux directories, by example

Preperation

Create 3 top-level directories. Create a file and a directory in each.

$ mkdir r rx x
$ touch {r,rx,x}/file
$ mkdir {r,rx,x}/dir
@ammaaim
ammaaim / yaml.rb
Last active December 12, 2018 12:41
Read/Write YAML in Ruby
require 'yaml'
$db_file = 'filename.yaml'
def load_data
text = File.read 'db.yaml'
return YAML.load text
end
def store_data hash
@SauloSilva
SauloSilva / each_slice.js
Last active May 8, 2019 16:09
Each slice javascript
// without callback
Array.prototype.eachSlice = function (size){
this.arr = []
for (var i = 0, l = this.length; i < l; i += size){
this.arr.push(this.slice(i, i + size))
}
return this.arr
};
[1, 2, 3, 4, 5, 6].eachSlice(2)
@littlefolk
littlefolk / gist:788723
Created January 20, 2011 21:35
javascript#each_cons
// https://developer.mozilla.org/ja/New_in_JavaScript_1.7#Array_comprehensions
function range (begin, end) {
for (let i = begin; i < end; ++i) {
yield i;
}
};
// http://code.activestate.com/recipes/347689-ruby-arrayeach_cons-each_slice-overlapping-and-non/
function each_cons (l, n) {
return [l.slice(i, i + n) for (i in (range(0, l.length - n + 1)))]
@lbvf50mobile
lbvf50mobile / README.md
Last active July 2, 2019 09:55
Greetings to @Morozzzko, the Ruby is neat.

Hi folks here I show some Ruby magic.

  • a.group_by{|x| x} is similar to a.group_by(&:itself)
  • a.group_by(&:itself) is a short cut for a.group_by(&:itself.to_proc)
  • and a.group_by(&:itself.to_proc) just use new Proc a.group_by(&Proc.new{|o| o.send :itself})
  • a.group_by(&Proc.new{|o| o.send :itself}) use & to treat a Proc as block like this a.group_by{|o| o.send :itself}
  • a.group_by{|o| o.send :itself} here it's possible to substitute o.send :itself to o.itself: a.gruop_by{|o| o.itself}
  • a.group_by{|o| o.itself} here o.itself it's just an o so it's equal to a.group_by{|o| o}
  • Hey and the magic is a.group_by{|o| o} is just a a.group_by{|x| x}
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
s = "".join([str(d) for d in digits])
i = int(s)
i = i + 1
s = str(i)
return [int(si) for si in s]
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
cur = head
# Component way of adding redis
# vendor/dry/system/components.rb
Dry::System.register_provider(
:persistence,
boot_path: Pathname(__dir__).join('components').realpath()
)
# vendor/dry/system/components/redis.rb
Dry::System.register_component(:redis, provider: :persistence) do
init do
@primaryobjects
primaryobjects / bst-delete-node.js
Created January 22, 2017 19:43
Delete a node in a Binary Search Tree BST.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} key
@Integralist
Integralist / 1. sinatra-basic-with-comments.rb
Last active November 9, 2019 20:10
Create basic site using Ruby and Sinatra (and external templates)
#!/usr/bin/env ruby
=begin
install Sinatra: gem install sinatra
install Shotgun: gem install shotgun (this auto-reloads sinatra on every http request - which means every time you make a change in your code you don't have to stop then start sinatra)
To just run your code using Sinatra: ruby name-of-file.rb
To run your code using Shotgun (which is just Sinatra but with ability to auto-reload when changes are made to files): shotgun name-of-file.rb
The following examples are run using Shotgun and the URL is: http://127.0.0.1:9393/