Skip to content

Instantly share code, notes, and snippets.

@Dawenster
Last active December 13, 2015 22:19
Show Gist options
  • Save Dawenster/4983448 to your computer and use it in GitHub Desktop.
Save Dawenster/4983448 to your computer and use it in GitHub Desktop.
Some useful coding stuff! Must review constantly...
ARRAYS---------------------------------------------------------------------------------------------
Q: How to easily create an array of strings?
x = %w[a b c d]
Q: How to pull out items in an array and use that instead of the original array
array.select!{ |x| x.prime? }
Q: Are .map and .collect the same thing?
Basically.
HASHES---------------------------------------------------------------------------------------------
Q: How to turn an array into the keys of a hash?
a = [1,2,3,4]
b = %w[a b c d]
x = a.zip(b) #This is an intermediary to create an array of arrays
h = Hash[x]
Q: How to turn an array into a pure hash?
hash = Hash[*hash.flatten]
SUBLIME TEXT----------------------------------------------------------------------------------------
Q: How do I make the colours all ruby-friendly?
Ctrl + Shift + "P"
Type "ruby"
Hit enter
TERMINAL-------------------------------------------------------------------------------------------
Q: How do I quickly get to the folder of the file I'm working on?
Drag the file from finder directly into the terminal
GENERAL---------------------------------------------------------------------------------------------
Q: What's a one-line way of writing boolean if-else statements?
boolean ? true : false
Q: What's a quick way to identify an empty method?
def happy; end
Q: How can you make big numbers easier to read?
1 million is the same as 1_000_000
Q: How can you chain something on separate lines?
def square_times(array)
array.find_all do |x|
(2..x-1).select do |i|
x % i == 0
end.count == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment