Rails vs The Client Side

Great talk by Noel Rappin on Rails vs the Client Side (i.e. JavaScript MVC frameworks such as EmberJS).

Contrasted are: 

37Signals “Russian Doll” approach (the server tends to return HTML to the client and uses aggressive caching and some JavaScript glue to keep the application fast). 

The “Rails API” approach, where the server returns JSON to the client, and a JavaScript MVC framework handles the actual display. 

More here:

http://www.confreaks.com/videos/2447-railsconf2013-rails-vs-the-client-side

Ruby: instance methods

Want to see just the methods specific to your instance?

instance_methods returns all the methods belonging to your instance. This includes methods of its superclass, e.g.

> i = Integer

> i.instance_methods

 => [:integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :-@, :<=>, :eql?, :quo, :fdiv, :div, :divmod, :%, :modulo, :remainder, :abs, :magnitude, :real?, :zero?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :==, :>, :>=, :<, :<=, :between?, :nil?, :===, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__] 

but you can rule out the superclass methods by passing in ‘false’ as a parameter to instance_methods. E.g.

> i.instance_methods(false)

 => [:integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize] 

http://www.ruby-doc.org/core-2.0/Object.html#method-i-methods

Ruby: proc {}.arity – a second go…

arity returns the number of arguments that would not be ignored.

e.g. if the block is declared to take 0 arguments it returns 0.

If the block takes n arguments, it returns n.

If the block takes optional arguments it returns -n-1 where n is the number of mandatory arguments.

e.g. 

proc {}.arity          #=>  0

proc {||}.arity        #=>  0

proc {|a|}.arity       #=>  1

proc {|a,b|}.arity     #=>  2

proc {|a,b,c|}.arity   #=>  3

proc {|*a|}.arity      #=> -1

proc {|a,*b|}.arity    #=> -2

proc {|a,*b, c|}.arity #=> -3

Note that:

proc{|a=1||}.arity #=> 0

This is because the `a` is now optional.

http://www.ruby-doc.org/core-2.0/Proc.html#method-i-arity

And for proc vs lambda.arity

e.g. 

proc   { |x = 0| }.arity       #=> 0

lambda { |a = 0| }.arity       #=> -1

proc   { |x=0, y| }.arity      #=> 1

lambda { |x=0, y| }.arity      #=> -2

Ruby: difference between proc and lambda

A proc and a lambda are very similar except:

1. they handle arguments differently

proc {} 

and

Proc.new {}

are equivalent.

However, using lambda {} gives you a proc that checks the number of arguments passed to it.

See: 

http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-lambda

and:

http://stackoverflow.com/questions/1740046/whats-the-difference-between-a-proc-and-a-lambda-in-ruby

2. they handle return and break statements differently

def bar 

f = lambda { return “from inside lambda”}

f.call

return “return from bar”

end

puts bar

def foo

f = Proc.new { return “from inside proc”}

f.call

return “return from foo”

end

puts foo

gives:

return from bar

from inside proc

See

http://www.akshay.cc/blog/2010-02-14-control-flow-differences-between-ruby-procs-and-lambdas.html

and

http://www.slideshare.net/MatthieuSegret/ruby-block-proc-and-lambda

Note that the behaviour of proc and lambda changed between Ruby 1.8 and Ruby 1.9 (and therefore 2.0). 

In 1.8, they were identical (both had lambda semantics). 

proc was like lambda in 1.8. However, it’s like Proc.new in 1.9

See also this post which summarises the difference as follows:

“In short: lambdas behave like methods, non-lambda Procs behave like blocks.”

http://stackoverflow.com/questions/4799964/differences-between-proc-and-lambda-in-ruby

 

Ruby: difference between proc and lambda

A proc and a lambda are very similar except:

1. they handle arguments differently

proc {} 

and

Proc.new {}

are equivalent.

However, using lambda {} gives you a proc that checks the number of arguments passed to it.

See: 

http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-lambda

and:

http://stackoverflow.com/questions/1740046/whats-the-difference-between-a-proc-and-a-lambda-in-ruby

2. they handle return and break statements differently

def bar 

f = lambda { return “from inside lambda”}

f.call

return “return from bar”

end

puts bar

def foo

f = Proc.new { return “from inside proc”}

f.call

return “return from foo”

end

puts foo

gives:

return from bar

from inside proc

See

http://www.akshay.cc/blog/2010-02-14-control-flow-differences-between-ruby-procs-and-lambdas.html

and

http://www.slideshare.net/MatthieuSegret/ruby-block-proc-and-lambda

Note that the behaviour of proc and lambda changed between Ruby 1.8 and Ruby 1.9 (and therefore 2.0). 

In 1.8, they were identical (both had lambda semantics). 

proc was like lambda in 1.8. However, it’s like Proc.new in 1.9

 

Rails: assets

By default, Rails assumes your assets are precompiled in the production environment. 

And this gives you the best performance.

To check, config.assets.compile should be set to false, e.g.

# config/environments/production.rb

config.assets.compile = false    # don’t compile at runtime. i.e. assets are precompiled

However, you will need to make sure your assets are precompiled. e.g. 

bundle exec rake assets:precompile

A bit of a pain if you have to do it manually. Fortunately, Capistrano has a recipe to handle this at deploy time.

http://stackoverflow.com/questions/7275636/rails-3-1-0-actionviewtemplateerrror-application-css-isnt-precompiled

Notes:

1. you can disable the asset pipeline while creating a new application by passing the —skip-sprockets option

2. You must have an ExecJS supported runtime in order to use CoffeeScript

More on the Asset pipeline here:

http://guides.rubyonrails.org/asset_pipeline.html

AutoHotKey alternative for Mac: KeyRemap4MacBook

Complex but powerful.

Don’t use it unless you’re prepared to fiddle with XML and read the docs. 

However, if you’re prepared to put in an hour you’ll reap the benefits – very useful.

Download it from:

https://pqrs.org/macosx/keyremap4macbook/index.html.en

and edit the private.xml using:

Preferences > Misc & Uninstall > Open private.xml

These are handy:

Examples:

https://pqrs.org/macosx/keyremap4macbook/xml.html.en#examples

The Google Group:

https://groups.google.com/forum/#!forum/keyremap4macbook

A sample of how to do a key sequence:

https://raw.github.com/tekezo/Files/master/KeyRemap4MacBook/private.control_d_to_kill_line/private.xml

List of KeyCodes:

https://github.com/tekezo/KeyRemap4MacBook/blob/version_8.3.0/src/bridge/generator/keycode/data/KeyCode.data

List of ModifierFlags:

https://github.com/tekezo/KeyRemap4MacBook/blob/version_8.3.0/src/bridge/generator/keycode/data/ModifierFlag.data

Less handy (unless you want to plough through issues):

https://github.com/tekezo/KeyRemap4MacBook/issues

E.g. to nudge cell contents to the next cell to the right, I use this (replaces Command-X, right cursor, Command-V):

<?xml version=”1.0″?>

<root>

<item>

<name>Excel: Quick Cut and Paste</name>

<identifier>private.excel_cut_paste</identifier>

<autogen>

__KeyToKey__

KeyCode::CURSOR_RIGHT, ModifierFlag::OPTION_L | ModifierFlag::SHIFT_L, 

KeyCode::X, ModifierFlag::COMMAND_L,

KeyCode::CURSOR_RIGHT,

KeyCode::V, ModifierFlag::COMMAND_L,

</autogen>

</item>

</root>

Don’t forget to:

– select your custom shortcut in Preferences > Change Key

– and add it to your Login items

 

AutoHotKey alternative for Mac: KeyRemap4MacBook

Complex but powerful.

Don’t use it unless you’re prepared to fiddle with XML and read the docs. 

However, if you’re prepared to put in an hour you’ll reap the benefits – very useful.

Download it from:

https://pqrs.org/macosx/keyremap4macbook/index.html.en

These are handy:

Examples:

https://pqrs.org/macosx/keyremap4macbook/xml.html.en#examples

The Google Group:

https://groups.google.com/forum/#!forum/keyremap4macbook

A sample of how to do a key sequence:

https://raw.github.com/tekezo/Files/master/KeyRemap4MacBook/private.control_d_to_kill_line/private.xml

List of KeyCodes:

https://github.com/tekezo/KeyRemap4MacBook/blob/version_8.3.0/src/bridge/generator/keycode/data/KeyCode.data

List of ModifierFlags:

https://github.com/tekezo/KeyRemap4MacBook/blob/version_8.3.0/src/bridge/generator/keycode/data/ModifierFlag.data

Less handy (unless you want to plough through issues):

https://github.com/tekezo/KeyRemap4MacBook/issues