Skip to content

Instantly share code, notes, and snippets.

@sk187
sk187 / ruby_concatenation_operator.rb
Last active August 29, 2015 14:17
Ruby Concatenation (Append) Operator Example
#.Push Method Arrays
number = [1,2,3,4]
number.push(5)
#Returns
[1,2,3,4,5]
#Concatenation Method Arrays
number = [1,2,3,4]
number << 5
#Returns
@sk187
sk187 / ruby_proc.rb
Last active August 29, 2015 14:17
Procs in Ruby
#Procs allow you to DRY up your code so that
#you don't have to rewrite blocks in your code.
#Non Proc Example
even_numbers = [2,4]
odd_numbers = [1,3]
doubled_even_numbers = even_numbers.select{|number| number*2}
doubled_odd_numbers = odd_numbers.select{|number| number *2}
#Returns
@sk187
sk187 / passing_ruby_methods_as_a_proc.rb
Created March 26, 2015 15:36
Passing Ruby Methods as a Proc
#You can pass Ruby methods like a Proc
#Non Proc Example
numbers = [1,2,3,4,5]
string_numbers = numbers.map{ |number| number.to_s}
#Returns
["1","2","3","4","5"]
#Proc Example
numbers = [1,2,3,4,5]
@sk187
sk187 / lambda_vs_proc.rb
Last active August 29, 2015 14:17
Lambda Vs. Procs
#Lambdas and Procs are very similar but there are two difference
#1. Lambda check the # of params and will throw an error if its off
# but Proc will assign a nil to any missing params and ignore others
#2. Lambda returns control to the calling method but Proc does not
def lambda_vs_proc
winner = Proc.new { return "Proc wins!"}
winner.call
"Lambda wins!"
end
@sk187
sk187 / ruby_modules.rb
Last active August 29, 2015 14:17
Ruby Modules
# Modules are similar to classes but they can't create new instances
# nor can they have subclasses. They are used to store things such as
# methods and constants until we need them.
# Modules let classes "inherit" behaviors as needed also known as mixins
module Code
# Constants can be defined in modules in ALL CAPS with _ for multiple words
CURRENT_JOB = "programmer"
def code
@sk187
sk187 / ruby_inheritance.rb
Last active August 29, 2015 14:17
Ruby Inheritance
# Inheritance is an important idea for Object Orientated Programming OOP
# This example will demo a simple example of inheritance
class Dog
def talk
puts "Woof"
end
end
class JackRussell < Dog
@sk187
sk187 / ruby_module_include_vs_extend.rb
Created March 26, 2015 19:23
Ruby Module: Include vs. Extend
# Include give you access to a module on a instance level while
# extend give class level access.
module CanYouHearMe
def test
puts "I hear you loud and clear!"
end
end
class Radio
@sk187
sk187 / ruby_variable_scope.rb
Created March 27, 2015 15:19
Ruby Variable Scope
# Global
class Scope
$global = "I am a global variable"
@@class_variable = "I am a class variable"
def initialize(instance_variable)
@instance_variable = instance_variable
end
def instance_message
@sk187
sk187 / ruby_yield.rb
Created March 31, 2015 15:59
Ruby Yield
# Yield in Ruby allow you to pass a block implicitly which not only makes it faster
# to type out this pattern but it also faster to execute!
# Regular Block Syntax
def calculate(a, b, action)
action.call(a,b)
end
# Defining the lambda add and subtract
add = lambda {|a,b| a+b}
@sk187
sk187 / install_nlpk_lda.py
Last active August 29, 2015 14:18
Install NLPK, LDA and Beautiful Soup for Sublime REPL
# This guide is for people who have install Sublime REPL and linked their system Python to it.
# If you haven't but want to, refer to my previous post where I show you how.
# If you are using Python 3 and still have Python 2 installed on your system as default
# For NLTK
python3 -m pip install -U nltk
# For LDA
python3 -m pip install lda