1 - Splat operator (“*”)

Entry in arg list “soaks up” list of args

http://www.catb.org/jargon/html/S/splat.html

CAN BE USED TO FIND LIST OF ARGS PASSED TO BLOCK:

unknown_block do |*args|
  args.count
  args[0].class
end

[CoffeeScript initially used the “*VAR” then changed to “VAR…” github.com/jashkenas/coffee-script/issues/45]

1b - Block/Method parameters

Variable in pipes at the start of the block/method

2 - Double-bang operator (“!!”)

The “!” converts the value into a boolean and then negates it.

The extra "!" negates the negated boolean eg "!! completed_at" -> "if completed"

3 - Namespace resolution operator (“::”)

module SomeModule
 module InnerModule
  class MyClass
   CONSTANT = 4
  end
 end
end

SomeModule::InnerModule::MyClass::CONSTANT

4 - Scope resolution operator

.  = instance method     (or message operator)
:: = class/module method (or scope opertor)

(See http://en.wikipedia.org/wiki/Scope_resolution_operator#Ruby)

[Rails routes use '#' for 'Controller#Action']

5 - Method argument hash key syntactic sugar

Ruby has implicit hash parameters so enclosing “{}” not required (as in option 3)

(See 13 - Appending and prepending colon)

6 - &Symbol -> Symbol#to_proc

Impicit class cast applies 'Symbol#to_proc' to ':foo'

'bar(&:foo)' converted to 'bar{|x| x.foo}'

Can be used with 'map' on array of objects

'~/.rvm/gems/ruby-2.2.2/gems/fnordmetric-1.2.9/lib/fnordmetric.rb':
def self.register(obj)
  @@pool.push(obj)
end

def self.start_em
  EM.run do
    EM.next_tick do
      (@@pool || []).map(&:initialized)
    end
  end
end

7 - Send method

(See 'tdd.txt' for details about test doubles hooking messages)

Send a method message to an object

Like passing a function pointer to a middleware in C
'def method_missing' can be used to perform the appropriate action

8 - Variable syntax

9 - Require vs Include

Require when wanted methods are in another file ('require_relative' when in same directory)

Include when extending classes with other modules (ie mix-ins) eg “include Checkout” in 'spree/core/app/models/spree/order.rb'

10 - Yield

Yields to the associated block of code (implicitly passed to the method)

'block_given?' prevents 'yield' giving an error on no block implicitly passed

11 - RDoc of installed gems

$ gem server;

12 - Lambda syntax in Ruby 1.9

'lambda' (ie anonymous functions like 'proc') can now be specified with '->' (or the 'stab' operator or “dash rocket”). Eg:

> foo2 = ->(arg) { arg*2 }
> foo2.call "now"
 => nownow

Note the lack of space between -> and (arg).

“->(x) { x * 2 }” SAME AS “lambda { |x| x * 2 }”

('=>', or “fat comma”, “hash rocket”, binds key-value pairs in a hash)

13 - Appending and prepending colon

New syntax for Ruby 1.9

(“Rails 5.0 will target Ruby 2.2+ exclusively” so up until that release then Ruby 1.9.3+ can be used, edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#ruby-versions)

Both are forms of a symbol

Prior to Ruby 1.9 hash's were defined with more verbose syntax:

hash = {
  :key => "value",
  :another_key => 4
}

Now can use:

hash = {
  key: "value",
  another_key: 4
}

(See 5 - Method argument hash key syntactic sugar)

14 - Open classes via Override or monkey-pathing

Ruby classes are open

ruby-journal.com/how-to-open-slash-override-slash-monkey-patch-a-class-in-ruby/

Can override existing class if already exists (just define it if not)

http://docs.ruby-lang.org/en/2.0.0/syntax/methods_rdoc.html#label-Overriding

'class_eval' opens existing class

If class does not exist then NameError exeception will be raised
Therefore extra check rather than overriding/defining class

15 - Find where method defined

'debugger' or '<% debugger %>'

16 - Sinatra and Rack

(28/8/16)

Rack

rack.github.io/

Rack, a modular Ruby webserver interface.

Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby.

By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, 
web frameworks, and middleware into a single method call.

Sinatra

www.sinatrarb.com/intro.html

Sinatra is a small “framework” on top of Rack

Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort

17 - Debugger

(28/12/16)

www.tutorialspoint.com/ruby/ruby_debugger.htm

18 - Pattern matching

(18/2/17)