Monday, February 29, 2016

Upgrading from Passenger 4.0.60 to 5.0.25

If you are managing your Rails 4.2.5 server using Moonshine, follow these steps.

Step 1

In passenger.rb, within Moonshine::Manifest::Rails::Passenger module, change the passenger version:

```ruby
BLESSED_VERSION = '5.0.25'
```

Step 2

Add the following Capistrano 2 task to deploy.rb:

namespace :deploy do
  task :restart, :roles => :app, :except => { :no_release => true } do
    sudo "passenger-config restart-app --ignore-app-not-running /srv//current"
  end
end

Checkin the code changes to git and deploy the app using Capistrano.

Step 3

On the server, verify the installed version:

```sh
passenger-config --version
```

Page Load Time

In passenger 4.0.60, it took 2.4 s
In passenger 5.0.25, it took 1.7 s


Turn on Keepalive to Speed Up Rails 4.2.5 App

I am using Moonshine to manage Rails 4.2.5 app RubyPlus on Linode. In Moonshine source code, file apache.rb, change the keep_alive to 'on':

 defaults = {
    :keep_alive => 'on',
 
Linode provides 2 GB of RAM for just $20/month. So, there is no reason not to turn on this setting. Especially for low traffic sites. This setting will keep the connection open so that all assets can be downloaded in one connection. This avoids opening multiple connection to the server by the browser to download the assets.

Moonshine Support for Passenger 5

1. Passenger 5 support added in : lib/moonshine/manifest/rails/passenger.rb
2. To restart the app with capistrano 2 add the following to your config/deploy.rb file:

namespace :deploy do
  task :restart, :roles => :app, :except => { :no_release => true } do
    sudo "passenger-config restart-app --ignore-app-not-running /srv//current"
  end
end


https://github.com/railsmachine/moonshine/pull/240

Using Custom Web Fonts in Rails 4.2.5 Apps


I was getting this error after hooking up Amazon Cloudfront CDN for RubyPlus. Here are the steps to make Rails 4.2.5 use custom fonts.

1. In:

config/application.rb 

config.assets.paths << Rails.root.join("app", "assets", "fonts")

2.  In your .scss files, change the src to use font-url not url:

@font-face {
    font-family: 'icofonts';
    src:font-url('fonts/icofonts.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}   
3.  In Moonshine source code, file apache.rb, add 'application/x-font-ttf', 'font/ttf' to the end of the gzip_types:

 defaults = {
    :gzip_types => ['text/html', 'text/plain', 'text/xml', 'text/css', 'text/javascript', 'text/json', 'application/x-javascript', 'application/javascript', 'application/json', 'application/x-font-ttf', 'font/ttf' ]


Reference:

Custom Web Fonts and the Rails Asset Pipeline



Thursday, February 25, 2016

retry

attempts = 0
begin
  raise
rescue
  attempts += 1
  puts 'retrying...'
  retry if attempts < 3
end


def work
  with_retry do
    puts 'working...'
    raise
  end
end

def with_retry
  attempts = 0
  begin
    yield
  rescue
    attempts += 1
    puts 'retrying...'
    if attempts < 3
      retry
    else
      puts 'Giving up!'
    end
  end
end

work

Tuesday, February 23, 2016

Performance of a.length > 0 vs a.empty? on

In Ruby 2.3.0

require 'benchmark'

a = []

t1 = Benchmark.realtime do
 a.length > 0
end

t2 = Benchmark.realtime do
  a.empty?
end

p t2 < t1


This prints true.



Tuesday, February 16, 2016

Symbol vs String

Symbol is like country names and string is like city names that are not unique. There is Richmond city in California as well as Virginia. This is an example for strings that have the same content but are in different memory locations. State name is unique, there is only one california in USA.

Filter out sensitive parameters from logging file in Rails 4.2.5

Create filter_parameter_logging.rb in config/initializers folder:

Rails.application.config.filter_parameters += [:password, :password_confirmation]





Tuesday, February 02, 2016

Thoughts on Sandi Metz presentation

Returning nil in the absence of some object means the type changes. Not all of them are ducks. One of them is going to be NilClass. This will not be able to respond to the message sent by the client.

This problem is because the return type from a method is not uniform. If it always returns an object that can respond to a certain message sent by a client, then the program will not crash.

In Java, we can declare interface. Something like this:

interface OperateCar {
  int turn(direction)
}

You can see that the return type of the turn method is integer. The clients can depend on this fact. They need not worry about handing a null for instance. Since the interface is implicit in Ruby, it allows returning different types of objects, this leads to problems. The only way to make the client code simpler is to make the implementation return something that has consistent interface.

What are the components of an interface?
How can we write code to express the interface ?
Seems like the code will be implicit when it has to express the uniformity or the protocol.
What is a protocol?
What is an interface?

Monday, February 01, 2016

Dave Thomas Presentation Notes

Class names are constant. Why?
Classes are objects.
Class name can be assigned to a variable and used to create an instance.

irb
2.2.4 :001 > String.name
 => "String"
2.2.4 :002 > Class.name
 => "Class"
2.2.4 :003 > BasicObject.name
 => "BasicObject"
2.2.4 :004 > self.name
NoMethodError: undefined method `name' for main:Object
    from (irb):4
    from /Users/bparanj/.rvm/rubies/ruby-2.2.4/bin/irb:11:in `
'
2.2.4 :005 > self.to_s
 => "main"

 String.to_s
 => "String"
2.2.4 :007 > Class.to_s
 => "Class"
2.2.4 :008 > BasicObject.to_s
 => "BasicObject"


Instance variables are stored in current instance.


In the middle of the class, the code is running.
Every method call has a receiver.


puts does not have explicit receiver.
Default object, is always the self (the current object)
We did not create any object. We ask main :
What is the class that is used to create an instance of main?


Methods are not objects. It can be converted to an object.
puts can be converted into an object and we can call puts.

What is the current object?

Example.

No compilation phase.
Definitions are active.
There is always a receiver.

Class definitions is executable.
Everything gets executed.


Instead of doing:

Calling an utility method:

Encryptor.encrypt('secret')

vs

'secret'.encrypt

Sending a message

module Some

end

Some.new

vs

Module.new

class Object


  def to_s
    'main'
  end
end


class Person

  puts self
  puts self.class

end

class Person

  def self.talk
  end

end


class Person

  def self.talk
  end

  Person.talk
end



class Person

  def self.talk
  end

  self.talk

end


class Person

  def self.talk
  end

  talk

end


class Person

  def self.talk
  end

end

Person.talk


class Dumbass < Person

  talk

end

Notes from Smalltalk Presentation by Dan

The real power is in the messaging. The message name represents what and the implementation is hidden behind the method.

Break complex problem down into:

As few parts as possible as independent as possible.
Easier to learn because fewer components.
Productive because the components are re-usable.

Simplicity and Generality

Analysis

Small number of independent parts

Synthesis

Easy to learn
Productive
Maintainable (Built with fewer components)

factorial
self <= 1
  ifTrue: []
 
 
self = 0 ifTrue: [^ 1].
self > 0 ifTrue: [^ self * (self - 1) factorial].
self error: 'Not valid for negative integers'


recFact
self < 0
     ifTrue : [^0]
     ifFalse: [
         self = 0
             ifTrue : [^1]
             ifFalse: [^self * (self -1) recFact]
              ]


Instead of :

loop do
 p 'This will print forever'
end

[ code ] repeat

Ruby Equivalent

(Block Object).repeat

Ignore knowledge about objects. Focus on messages.


Smalltalk