Atom Hax: Installing Rubocop For Atom

I have recently been trying out Atom text editor and noticed that I couldn’t get Rubocop to work. I kept getting:

Error: env: ruby_executable_hooks: No such file or directory

After some quick searching, the following solution came up on SO. Basically you need to go back to your ~/.atom/config.cson and make the following changes.

 

# This is what your probably have
"linter-rubocop":
command: "/Users/sungkim/.rvm/gems/ruby-2.2.3/bin/rubocop"
# This is what you should have
# Notice that the bin was changed to wrapper
"linter-rubocop":
command: "/Users/sungkim/.rvm/gems/ruby-2.2.3/wrappers/rubocop"

view raw
rubocoprvm.cson
hosted with ❤ by GitHub

Fixed everything for me and now it works like a charm!

CodeHax: Code to Deploy – Part one. Installing Homebrew, Ruby, Git & Sublime

I just recently taught an intro to Ruby on Rails class and the hardest part was not explaining data types or object oriented programming but how to install everything you need to begin with. If you are using a Mac, the good and bad news is it already has Ruby installed.

While you can play around with Ruby out of the box, if you want to do something more serious with it such as Rails, you will need to install the latest version & tell your computer to use that path rather than the one already on your system.

But before we even get there, using a package manager to download, maintain and easily install/uninstall other programs is also super helpful. For this, we are going to use Homebrew.

Step 1: Find the Terminal
First, you need to find your terminal (also known as a command prompt or command Line interface CLI).

Step 2: Install Homebrew

# Type this into your terminal. You will notice that we are actually using Ruby to install Homebrew.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

view raw
homebrew.sh
hosted with ❤ by GitHub

You will probably get a prompt asking you if you will install xCode. Its a large file and there are ways to not install it but Homebrew depends on xCode so just install it.

Step 3: Install Ruby

# First type this into your terminal. *You must already have homebrew installed
brew install ruby
# After installing ruby, we need to edit the path on your computer so it uses
# the new installation of ruby and not the one packaged with your computer.
# Type the following into your terminal to edit the paths with VIM
sudo vi /etc/paths
# It will ask for your password (the one you used to login / install programs)
# Just type it in and press enter *You will not see anything but you are entering something I promise.
# This is what you will probably see
/usr/bin
/bin
/usr/sbin
/sbin
# We need to edit the paths to look like this:
/usr/local/bin # This is where your brew installed programs are
/usr/bin # This is where the system version of Ruby is
/bin
/usr/sbin
/sbin
# Once you have made these changes, you need to hit ESC, then :w (write) to save the file
# Once saved, you can type :q (quit) to exit the text editor

Step 4: Install Git

brew install git # This will install the latest version of Git into your Homebrew Cellar

Step 5: Checking Git and Ruby installations

# Into your temrminal type
which ruby # This will give you the path to where ruby is installed if installed
ruby -v # This will give you the version of ruby you currently have installed
# You can check just about any other program in the same fashion
which git
git -v

view raw
check_ruby_git.sh
hosted with ❤ by GitHub

Step 6: Install Sublime Text
Pretty straight forward. Sublime is a text editor. This where we will edit code.
go to here and download it.

Part 2 will cover scaffolding a basic CRUD function application in Rails as well as Postgresql.

Ruby Hax: 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}
subtract = lambda {|a,b| ab}
puts calculate(10,5, add)
puts calculate(10,5, subtract)
# Returns
"15"
"5"
#Yield Syntax
def calculate(a, b)
yield(a,b)
end
puts calculate(10,5){|a,b| a+b} #Add
puts calculate(10,5){|a,b| ab} #Subtract
# Returns
"15"
"5"

view raw
ruby_yield.rb
hosted with ❤ by GitHub

Ruby Hacks: Include vs Extend for Ruby Modules

# 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
include CanYouHearMe
end
class Satellite
extend CanYouHearMe
end
# Calling the method test on the class Radio
Radio.test
# Returns
"private method 'test' called for Context::Radio:Class"
# Calling the method test on the class Satellite
Satellite.test
# Returns
"I hear you loud and clear!"
# Calling the method test on an instance of the class Radio
ham_radio = Radio.new
ham_radio.test
# Returns
"I hear you loud and clear!"
# Calling the method test on an instance of the class Satellite
sat_comm = Satellite.new
sat_comm.test
# Returns
"private method 'test' called for #<Context::Satellite:0x000000024ef2f8>"

Ruby Hacks: Class inheritance in Ruby

# 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
end
class BichonFrise
end
roo = JackRussell.new
roo.talk
#Returns
"Woof"
candy = BichonFrise.new
candy.talk
#Returns
" undefined method `talk' "
# Notice that the class JackRussell inherits from the class Dog on line 10.
# While JackRussell has no talk method defined, it inherits the talk method
# from the parent or Super class of Dog. This is why roo.talk returns "Woof"
# Meanwhile candy.talk returns undefined method as it is talk is not defined
# in the class BichonFrise nor did it inherit from the class Dog (line 21)

view raw
ruby_inheritance.rb
hosted with ❤ by GitHub

Ruby Hax: Variable Scope in Ruby

# 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
@instance_variable
end
def self.class_message
@@class_variable
end
end
# Creating a new instance of the Scope class called example
example = Scope.new("I am an instance variable")
# $global is a global variable so it can be called anywhere
puts $global
# example is a instance of the Scope class. We set the input "I am an instance variable" to the instance variable
# @instance_variable in the initialize method. We then put @instance_variable into a method called instance_message.
puts example.instance_message
# @@clasS_message is a class variable which means it is accessable to the entire class. Notice we also had to
# create a class method with self.class_message as it is not an instance method like instance_message
puts Scope.class_message
# Returns
"I am a global variable"
"I am an instance variable"
"I am a class variable"

Ruby Hax: What is a Module?

# 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
# #{@name} is allowing me to display @name's attribute. If it was just @name, the puts
# method would return @name as a string, not the attribute of @name
# "Because @name is a programmer, @name is coding."
puts "Because #{@name} is a #{CURRENT_JOB}, #{@name} is coding."
end
end
class Person
# the include Code allows the class Person to have access to the module Code
include Code
def initialize(name)
@name = name
end
end
# Creating a new instance of Person
sung = Person.new("Sung")
sung.code
# Returns
"Because Sung is a programmer, Sung is coding."

view raw
ruby_modules.rb
hosted with ❤ by GitHub

Ruby Hax: What the heck is the difference between Proc and Lambda?

#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
puts lambda_vs_proc
#Returns
"Proc wins!"
def proc_vs_lambda
winner = lambda {return "Lambda wins!"}
winner.call
"Proc wins!"
end
puts proc_vs_lambda
#Returns
"Proc wins!"
#In the first example, Proc takes control and returns immediately.
#In the second example, Lambda returns control to the method which hits "Proc wins!"
# and returns the last line not "Lambda wins!"

view raw
lambda_vs_proc.rb
hosted with ❤ by GitHub