Skip to content

Instantly share code, notes, and snippets.

View samuraijane's full-sized avatar

Matthew Day samuraijane

View GitHub Profile
@samuraijane
samuraijane / 0_reuse_code.js
Last active August 29, 2015 14:17
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@samuraijane
samuraijane / WP Output for <li>
Created March 23, 2015 19:57
This code defines the output for <li> in version 4.1. I researched this to figure out how to override the ID and classes that WP assigns to menu items contained in an <li>. One solution is to write your own walker to override this. Another is to simply delete the output parameters in line 15 (although this is a bad idea). See wp-includes/nav-men…
/**
* Filter the ID applied to a menu item's list item element.
*
* @since 3.0.1
* @since 4.1.0 The `$depth` parameter was added.
*
* @param string $menu_id The ID that is applied to the menu item's `<li>` element.
* @param object $item The current menu item.
* @param array $args An array of {@see wp_nav_menu()} arguments.
* @param int $depth Depth of menu item. Used for padding.
@samuraijane
samuraijane / Animate display: none
Last active August 29, 2015 14:17
Since CSS cannot animate a <div> with display: none to another <div> with display: block, you need to use jquery.
// remap jQuery to $
(function($){
// Can also be used with $(document).ready()
$(window).load(function() {
$('#navs-menu').click(function () {
$('.nav-menu').slideToggle(300);
@samuraijane
samuraijane / Define variable within `each`
Created March 25, 2015 00:36
An `each` statement is basically a loop in which you can define a variable. However, you can only use that variable within the statement itself. If you need that variable outside of the statement, use `do`.
#Each statements do not allow their variables to be used outside of the statement.
<%= friends.each{|friend| friend['children']} %>
#To do this use `do`.
<% friends.each do
|friend| @uno = friend['name']
end %>
<%= "#{@uno}" %>
@samuraijane
samuraijane / For versus Each
Created March 25, 2015 03:01
Each is considered better form than a for loop. Although they achieve the same results, the variable in the for loop is retained after it executes (which can make debugging more difficult) while the variable in each only lives inside the block.
<%
for item in ids
puts item
end
%>
<%
ids.each do |item|
puts item
end
@samuraijane
samuraijane / Nested Maps.rb
Created March 25, 2015 06:35
The proper way to nest maps
<%= hobbiestostring = '[' + (friends.map{ |friend| (friend[ 'children' ]).map{ |nino| nino[ 'name'] }}).join(", ") + ']' %>
@samuraijane
samuraijane / to_s versus join.rb
Last active August 29, 2015 14:17
to_s and join are both used to convert to string but they are not exactly the same (research pending).
<%= 'filexyz ' + (filexyz.class).to_s %> #>filexyz String
<%= (['filexyz'] + [filexyz.class]).join(" ") %> #>filexyz String
@samuraijane
samuraijane / block_or_line.rb
Last active August 29, 2015 14:18
You can write using a block or write the code on one line when nesting maps. This code shows you how to do both.
#Code with block
$myvariable= imgarray.map do |layouts|
layouts[ 'layouts' ].map do |layout_name|
layout_name['layout_name']
end
end
#Code on one line
$myvariable= (imgarray.map{ |layouts| (layouts[ 'layouts' ]).map{ |layout_name| (layout_name[ 'layout_name' ]) }} )
@samuraijane
samuraijane / nested_map_variable.rb
Created March 31, 2015 21:54
When dealing with nested maps that create an array you need to access, where you define the variable that holds the array is important. See the differences here.
#Example 1
$myvariable= imgarray.map do |layouts|
layouts[ 'layouts' ].map do |layout_name|
layout_name[ 'layout_name' ]
end
end
#Placing the variable at the beginning of the loop returns an array within an array:
#[["annie", "billy", "charlie", "danny", "eddy", "fergie", "granny", "henry", "jenny", "kelsey"]]
#Example 2
@samuraijane
samuraijane / hash_key_options.rb
Created April 17, 2015 16:27
To pull just the keys out of a hash, you have a few options but they are not interchangeable.
# This works in IRB but I could not get it to output in a browser. The command
# 'puts' will not output to a browser but with other methods, doing something
# like '{ |x| x}' usually works but not here.
hash.each_key { |x| puts x }
#This works to output to a browser. The output is an array.
hash.key