Wednesday, December 23, 2015

Test Rails 4.2.5 ActionMailer Settings

require 'action_mailer'

ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
   :address   => "smtp.gmail.com",
   :port      => 587,
   :domain    => "rubyplus.com",
   :authentication => :plain,
   :user_name      => "bparanj@rubyplus.com",
   :password       => "password",
   :enable_starttls_auto => true
  }
ActionMailer::Base.view_paths= File.dirname(__FILE__)

class Mailer < ActionMailer::Base

  def daily_email
    @var = "var"

    mail(   :to      => "bparanj@something.com",
            :from    => "bparanj@szy.com",
            :subject => "testing mail") do |format|
                format.text
                format.html
    end
  end
end

email = Mailer.daily_email.deliver_now
puts email
email.deliver

In mailer/daily_email.html.erb and mailer/daily_email.text.erb

this is an html email

and this is a variable <%= @var %>

this is a text email

and this is a variable <%= @var %>


Monday, December 21, 2015

How to include URL Helpers in Decorators in Rails 4.2.5

Add :
 
include Rails.application.routes.url_helpers
 
to the decorator. For example:
 
class BugsDecorator < ApplicationDecorator
  include Rails.application.routes.url_helpers
end 

Wednesday, December 16, 2015

ack options to list only file names

-w - Search whole word
-l  - Only print the filenames of matching files
-r - Recurse into subdirectories

ack -lrw '< MySuperClassName' --ignore-dir={app/assets,log,spec,vendor,public,coverage,config}

You can chain this to xargs to open all the matching file for editing like this:

ack -lrw '< MySuperClassName' --ignore-dir={app/assets,log,spec,vendor,public,coverage,config} | xargs mate


Sunday, December 13, 2015

Rails Performance Tools Presentation Notes

Tools for Linux

lsof
strace
ltrace

Tools for C Code

perftools
gdb

Tools for Networks

tcpdump
ngrep

Tools for CPU Usage

perftools
perftools.rb

Tools for Memory Usage

bleak_house
gdb.rb
memprof

LSOF

List open files

lsof -nPp

-n : Inhibits the conversion of network numbers to host names.
-P : Inhibits the conversion of port numbers to names for network files

TCP Dump

Dump traffic on a network

tcpdump -i eth0 -s 0 -nqA
tcp dst port 3306

Mammals

Mammals have hair or fur
Nurse their young with milk
have lungs and need air to breathe
Mammals that live on land have 4 legs and ears that stick out
Warm Blooded

Eg : Polar Bear, Gorilla, Horses etc

How to commit all deleted files in Git without affecting other modified files?

git ls-files --deleted | xargs git rm

Implementing Abs using Objects



def absolute(x)
  if x > 0
    x
  elsif x == 0
    0
  elsif x < 0
    -x
  end
end

# p absolute(0)


class GreaterThan
  attr_reader :number

  def initialize(value, number)
    @value, @number = value, number
  end

  def evaluate
    result[@number.send(:>, @value).class]
  end

  private

  def result
    {TrueClass => @number}
  end
end

# c = GreaterThan.new(0, -2)
#
# p c.evaluate

class LessThan
  attr_reader :number

  def initialize(value, number)
    @value, @number = value, number
  end

  def evaluate
    result[@number.send(:<, @value).class]
  end

  private

  def result
    {TrueClass => -@number}
  end
end


# c = LessThan.new(0, -2)
#
# p c.evaluate


class EqualTo
  attr_reader :number

  def initialize(value, number)
    @value, @number = value, number
  end

  def evaluate
    result[@number.send(:==, @value).class]
  end

  private

  def result
    {TrueClass => 0}
  end
end


# c = EqualTo.new(0, 0)
#
# p c.evaluate


def obzolute(x)
   [GreaterThan.new(0, x), LessThan.new(0, x), EqualTo.new(0,x)].each do |predicate|
      unless predicate.evaluate.nil?
        return predicate.evaluate
      end
   end
end

p obzolute(0)
p obzolute(-1)
p obzolute(2)


Second Version

class Predicate
  def initialize(operator, value)
    @operator, @value = operator, value
  end
 
  def evaluate
    @value.send(@operator.to_sym, 0)
  end
 
  def result
    {'>' => @value, '==' => 0, '<' => -@value}
  end
 
end

p1 = Predicate.new('<', -1)

p (p1.evaluate and p1.result['<'])


p2 = Predicate.new('==', 0)

p (p2.evaluate and p2.result['=='])


p3 = Predicate.new('>', 2)

p (p3.evaluate and p3.result['>'])

MIT Scheme Console

$ ./mit-scheme console
MIT/GNU Scheme running under OS X
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2014 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Saturday May 17, 2014 at 2:39:25 AM
  Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116
;Warning: Invalid keyword: "console"
;Warning: Unhandled command line options: ("console")

1 ]=> 486

;Value: 486

1 ]=> (+ 137 349)

;Value: 486

1 ]=> (- 1000 334)

;Value: 666

1 ]=> (* 5 99)

;Value: 495

1 ]=> (/ 10 5)

;Value: 2

1 ]=> (+ 2.7 10)

;Value: 12.7

Combination = (operator operand1 operand2). The value of the combination is computed by applying the operator to the operands.

The prefix notation has the advantage of taking an arbitrary number of arguments. Making it easy to compute your tax deductions:

1 ]=> (+ 21 35 12 7)

;Value: 75

Calculate Mileage rate deduction:

1 ]=> (+ (* 57.5 200) (* 23 700))

;Value: 27600.

57.5 cents for each business mile driven and 23 cents for moving deduction. I drove 200 miles for business miles and 700 miles for moving. So I get a total deduction of $276 (since the answer is in cents).

This shows another advantage of prefix notation where we can have combinations of elements which are combinations.

Defining Variable

1 ]=> define size 2

;Syntactic keyword may not be used as an expression: #[keyword-value-item 13]
;To continue, call RESTART with an option number:
; (RESTART 1) => Return to read-eval-print level 1.

2 error> 
;Unbound variable: size
;To continue, call RESTART with an option number:
; (RESTART 4) => Specify a value to use instead of size.
; (RESTART 3) => Define size to a given value.
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.

3 error> 
;Value: 2

3 error> (define size 2)

;Value: size

3 error> size

;Value: 2

3 error> (* 5 size)

;Value: 10

3 error> (define pi 3.14159)

;Value: pi

3 error> (define radius 10)

;Value: radius

3 error> (* pi (* radius radius))

;Value: 314.159

3 error> (define circumference (* 2 pi radius))

;Value: circumference

3 error> circumference

;Value: 62.8318

Installing Mit Scheme LISP on Mac OS 10.7.5

Hit this URL http://ftp.gnu.org/gnu/mit-scheme/stable.pkg/9.2/mit-scheme-9.2-x86-64.tar.gz on the browser to download the file.

 cd Downloads/
 cd mit-scheme-9.2
 cd src
 ./configure --prefix=/usr/local
 make -j9 compile-microcode
 make install
 cd /usr/local/bin
 ./mit-scheme --version

You should see:

MIT/GNU Scheme microcode 15.3
Copyright (C) 2014 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Saturday May 17, 2014 at 2:39:25 AM
  Release 9.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118
  Edwin 3.116
Moriturus te saluto.

Saturday, December 12, 2015

Notes for 5 Hidden Gems of the Ruby Standard Library Presentation

Set
- It doesn't keep order
- It doesn't support duplicates
- It's very fast at finding elements


BLACKLISTED_IPS = %w(42.100.119.12 61.103.82.121)

def black_listed?(ip)
  BLACKLISTED_IPS.include?(ip)
end

vs

require 'set'

same code as before

Find the process name using the process id

ps -p 1 -o comm=

/sbin/launchd

-p : PID
-o : command name

In this case the process id is 1 and comm is the command name. The story behind why I had to use this command. MySQL server stopped working on my Mac. It was complaining that the server process had quit without updating the PID file. The PID file did not exist. So I created one and put process id 1 as the value. When I made an attempt to restart the MySQL server, the entire machine rebooted. It turns out that the process with id one is the launchd that starts all other child processes in Mac.

`$ERROR_INFO' not initialized

Delete:

--warnings

in .rspec file.

Friday, December 11, 2015

Neither Pony nor ActionMailer appear to be loaded so email-spec is requiring ActionMailer.

Add require statement for action_mailer before email_spec in spec_helper.rb:

require 'action_mailer'
require "email_spec"

Tuesday, December 08, 2015

Find files that was changed before certain number of days

1. Find all controllers that was changed 4 days ago.

find ./app/controllers -type f -mtime -4

2. Find all views that was changed 4 days ago.

find . -mtime -4 -name "*.haml" -print

Monday, December 07, 2015

How to automatically source ~/.bash_profile in Mac OS

Add the line:

source ~/.bash_profile

to the ~/.bashrc file. Open a new terminal to test whether it works.

Sunday, December 06, 2015

Form vs Structure



Form: The visible shape of something Structure: The arrangement of and relations between the elements of something complex. Form relates to the external shape – best thought of as a silhouette. It is visible. Structure is goes beyond the visible – it is the internal development and relationship between parts. It is about the internal skeleton and organs. Think of it as an X ray or CT scan.

Diagram for 3 Rules for Design Article

Diagrams for Articles





Friday, December 04, 2015

Find all files less than a certain size

find ./app/helpers -type f -size -50c | more

If you want to find files greater than a certain size

find ./app/helpers -type f -size +50c | more

Show All the Commits Behind the Master for a Given Branch in Git

This will prepend "git show" to the commit hash 
git rev-list your-branch-name --not master | sed 's/^/git show /'  | pbcopy

git rev-list rails424 --not master | sed 's/^/git show /'  | xargs git show

will show the diff for each of the commit hash.

Tuesday, December 01, 2015

How to check when a file was deleted in git

1. Check the log for a particular file.

git log -- app/views/articles/publish.html.erb

2. Copy the commit hash and do:

git show commit-hash

You can view the changes made to the files including the files that were deleted.