Skip to content

Instantly share code, notes, and snippets.

View mediter's full-sized avatar
🎯
Focusing

Yuan, Jun mediter

🎯
Focusing
View GitHub Profile
@mediter
mediter / range.swift
Last active May 22, 2021 07:43
[NSRange v.s. Range<String.Index>] #swift #nsrange #range
https://stackoverflow.com/a/30404532
As of Swift 4 (Xcode 9), the Swift standard library provides methods to convert between Swift string ranges (Range<String.Index>) and NSString ranges (NSRange). Example:
let str = "a👿b🇩🇪c"
let r1 = str.range(of: "🇩🇪")!
// String range to NSRange:
let n1 = NSRange(r1, in: str)
print((str as NSString).substring(with: n1)) // 🇩🇪
@mediter
mediter / ruby-array-sum.rb
Created October 19, 2018 02:39
[Ruby Array Sum] #ruby #iterator #sum
# you can call #sum iterator on it:
numbers = [1, 2, 3, 4, 5]
numbers.sum
# => 15
# N.B: In Ruby versions < 2.4, sum didn't exist. The idiomatic way to get the same result was with #reduce:
+__rvm_make:0> make -j 1
CC = gcc
LD = ld
LDSHARED = gcc -dynamiclib
CFLAGS = -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration -Wdivision-by-zero -Wdeprecated-declarations -Wextra-tokens -fno-common -pipe
XCFLAGS = -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT
CPPFLAGS = -I/usr/local/opt/libyaml/include -I/usr/local/opt/readline/include -I/usr/local/opt/libksba/include -I/usr/local/opt/openssl/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -I. -I.ext/include/x86_64-darwin17 -I./include -I.
DLDFLAGS = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -L/usr/local/opt/libyaml/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/libksba/lib -L/usr/local/opt/openssl/lib -install_name /Users/jun/.rvm/rubies/
@mediter
mediter / migration_rollback.rb
Created December 9, 2018 09:49
[Rollback Migration] #migration #database #rails #ruby
rails db:rollback STEP=1
# Is a way to do this, if the migration you want to rollback is the last one applied. You can substitute 1 for however many migrations you want to go back.
# For example:
rails db:rollback STEP=5
# Will also rollback all the migration that happened later (4, 3, 2 and also 1).
# In order to rollback a specific migration use:
@mediter
mediter / bash_for_touch_echo.md
Last active December 4, 2018 15:30
[bash for-loop with echo] #bash #for-loop #touch #echo

for-loop with echo

for model in "artists" "galleries" "paintings"
do
    mkdir -p $(echo views/api/v1/$model)
    for verb in "index" "show"
    do
        touch $(echo views/api/v1/$model/$verb.json.jbuilder)
 done
@mediter
mediter / wp_reg_sidebar.php
Last active July 11, 2018 07:18
[WordPress Sidebars] #WordPress #PHP #sidebar
register sidebar in functions.php
registering a sidebar is just defining a container
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'spage',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h3>',
'after_title' => '</h3>'
@mediter
mediter / wp-excerpt.php
Created July 10, 2018 08:18
[WordPress Post Excerpt and Truncation] #WordPress #Excerpt
<ul>
<?php
$args=array(
'cat' => 1, // 分类ID
'posts_per_page' => 10, // 显示篇数
);
query_posts($args);
if(have_posts()) : while (have_posts()) : the_post();
?>
@mediter
mediter / wp_dev_func.php
Created July 10, 2018 07:57
[WordPress Function in Parent and Child Theme] #WordPress #PHP #Theme
// To write a pluggable function, you simply enclose it in a conditional tag to check if a function with that name has already been run:
<?php
if ( ! function_exists ( 'my_function' ) ) {
function my_function() {
// Contents of your function here.
}
}
?>
Then when you come to write a function in your child theme which you want to override the one in the parent theme, you just give it the same name as the one in the parent theme:
@mediter
mediter / regex_matches.swift
Last active May 24, 2018 16:07
[RegEx Matches] #swift #regex
Swift 3 (Xcode 8)
```swift
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range)}
} catch let error {
@mediter
mediter / wp_archive_title.php
Created May 22, 2018 06:10
[Remove “Category:”, “Tag:”, “Author:” from the_archive_title] #wordpress #archive #page-title
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );