This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer