Skip to content

Instantly share code, notes, and snippets.

@LOZORD
Last active August 29, 2015 14:08
Show Gist options
  • Save LOZORD/ce1b6c0b8335e2167baf to your computer and use it in GitHub Desktop.
Save LOZORD/ce1b6c0b8335e2167baf to your computer and use it in GitHub Desktop.
An explanation of my one-liner for MLH.
# A while back, Major League Hacking (mlh.io) wanted to make a video
# similar to the "I Believe That We Will Win" videos.
# In the video, hackers will say the line, showing off our passion and diversity.
# Being a hacker first, an a normal human second, I took the request as a challenge.
# When I remembered that `say` was a thing in Terminal, I knew what I had to do...
# In this gist (and hopefully a video), I'll explain what my one liner means.
# Hopefully, you'll learn some awesome Ruby tricks along the way.
# If you are new to Ruby, http://feministy.github.io/ruby_basics/#/ is a great start.
# Here's my one-liner:
(a=%w(I believe that we will hack!)).each_index{|i|s=a[0..i].join(' ');puts s;`say -v R #{s};sleep 1`}
# Wowee! That's cool, but compact and weird, right? Let's break it down!
# Starting from the left
a = [] # a is an Array
puts %w(This is weird syntax, right?)
puts %w(percent w will turn whatever words are inside its parens into a word-split Array)
puts %w(tacocat racecar) == ['tacocat', 'racecar']
puts ['tacocat', 'racecar'] == 'tacocat racecar'.split(' ')
# Cool, so that first third-ish makes sense, right?
# It's creating an Array of words (the 'I believe that we will hack line') an assigning it to variable 'a'
# Ok, so now for that 'each_index'
# If you're coming from a C, C++, Java, or Python background, you're probably familiar with for-loops
# Ruby has for-loops, but the community encourages using Enumerator functions like:
# each, kind of like Java's "for (Thing t: myThingList)"
# each_index, just your basic "for (i = 0; i < arr.size; i++)"
# each_with_index, a combination of both
# If you're coming from one of those languages above, just think of these methods as taking a block of code
# with an induction variable (or two induction variables in the case of each_with_index).
# If you're a functional kind of dude/dudette,
# these methods are taking a lambda in the form of a function argument,
# which is in turn, applied to each member of the Enumerable type.
# Either way, it makes sense --> you're applying a chunk of code to an element in a list!
# Code chunks/lambda are called Blocks and they are the last argument to function calls.
# They can either be written using brace/{} syntax, or "do |*| ... end" syntax
# Brace syntax helps for quick one-liners (use semicolons to separate lines of code)
# For example:
palindromes = ['tacocat', "racecar", 'panama'] # double quotes and single quotes are practically equal in Ruby
# we have two induction variables, p and i
# basically, p == palindromes[i]
palindromes.each_with_index do |p, i|
unless p == p.reverse
puts 'Hey! ' + p + ' is not a palindrome!'
puts "The impostor #{p} was found at index #{i}" # string interpolation #hellyeah
end
end
# Ok, cool, so we are just iterating through the array a
# But still, that code block looks nasty. Let's take a look at that.
#The first thing is:
s = a[0..i].join(' ')
# First, we are creating a temporary "subarray" from elements of a from 0 to i
# In Ruby, we can specify this as a Range: 0..i
# a..b == [a,b], but a...b [a,b) (b isn't included in the second one)
# Then we join together the subarray with spaces (' ')
# This is basically the opposite of split:
# We get a single String back from an Array
foobar = ['peanut', 'butter', 'and', 'jelly']
sandwich = foobar.join(' ')
loudwich = foobar.each { |word| word.upcase! }.join('! ') + '!!!'
# This line gives us the build-up part of the chant.
# So as i grows, the length of our chant grows as well!
# puts is the normal way to print things to the console.
# It is Ruby's System.out.println. Notice the newline!
# Finally, we reached the last step of our code block
# One of my favorite recent discoveries in Ruby is the backtick (`)
`echo " :) Hello world! :) "`
# Backticks allow you to pipe commands to and from your Terminal
# Here's a quick example
# A Hash, not a block (think Javascript/JSON)
cool = { lang: 'Ruby', suffix: '.rb' }
puts "Let's look for #{ cool[:lang] } (#{ cool[:suffix] }) files!"
data = `ls -l`.split("\n")
# now filter out non-Ruby files
data.keep_if { |file_row| file_row.ends_with? cool[:suffix] }
# now print the result
data.each { |item| puts " #{ item } \n\t is in #{ cool[:lang] } <3" }
# So for my backtick section, I'm jusst using the Mac Terminal say command (with a non-default voice)
# To build the chant hype, I also throw in a sleep command,
# which bascially stops the computer from doing anything for 1 second.
puts 'And there you have it! Good luck and Ruby on!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment