Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
lbvf50mobile / gist_to_github_repo.md
Created November 2, 2019 15:59 — forked from ishu3101/gist_to_github_repo.md
Transfer a gist to a GitHub repository

Transfer a gist to a GitHub repository

clone the gist

git clone https://gist.github.com/ishu3101/6fb35afd237e42ef25f9

rename the directory

mv 6fb35afd237e42ef25f9 ConvertTo-Markdown

change the working directory to the newly renamed directory

cd ConvertTo-Markdown

@lbvf50mobile
lbvf50mobile / check-substring-starts-with.go
Created September 4, 2019 15:00 — forked from flaviocopes/check-substring-starts-with.go
Go: check if a string starts with a substring #golang
package main
import (
"strings"
)
func main() {
strings.HasPrefix("foobar", "foo") // true
}

Lambda: standard

# Creating a lambda
l = lambda { |name| "Hi #{name}!" }

# Executing the lambda
l.call("foo") # => Hi foo!
// 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 / each_slice.js
Created May 2, 2019 11:11 — forked from SauloSilva/each_slice.js
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)
@lbvf50mobile
lbvf50mobile / gist:a90665a0e927a1be009706fe6ca2bc7a
Created April 3, 2019 15:26 — forked from mozamimy/gist:52c0004c8370f78df2c2
Validations for IP address and MAC address on Ruby on Rails
require "resolv"
class Model < ActiveRecord::Base
validates :ip_address, format: { with: Resolv::IPv4::Regex }
validates :mac_address, format: { with: /\A([0-9A-F]{2}[-:]){5}([0-9A-F]{2})\z/ }
end
@lbvf50mobile
lbvf50mobile / fileutils_example.rb
Created January 9, 2019 19:18 — forked from jensendarren/fileutils_example.rb
An example of using FileUtils in Ruby
require 'fileutils'
# __FILE__ keyword in ruby
puts "The current ruby file being executed is #{__FILE__}"
# Gets the directory name of the file being executed
current_directory = File.dirname(__FILE__)
puts "The current directory is #{current_directory}"
# expands to reveal the pwd (Present working directory)
@lbvf50mobile
lbvf50mobile / 1. sinatra-basic-with-comments.rb
Created December 31, 2018 11:44 — forked from Integralist/1. sinatra-basic-with-comments.rb
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/
@lbvf50mobile
lbvf50mobile / yaml.rb
Created December 12, 2018 12:41 — forked from ammaaim/yaml.rb
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
@lbvf50mobile
lbvf50mobile / benchmark_building_a_hash.rb
Created October 27, 2018 16:13 — forked from jonatack/benchmark_building_a_hash.rb
Benchmark building a Ruby hash: #each - #each_with_object - #reduce - Hash[map] - #map.zip(map).to_h - #reduce-merge
require 'benchmark/ips'
Benchmark.ips do |x|
Property = Struct.new(:name, :original_name)
PROPERTIES = [
Property.new("Clo", "Chloe" ),
Property.new("Jon", "Jonathan" ),
Property.new("Kris", "Kristin" ),