Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

Kernel


48 | Chapter 2: ActiveSupport and RailTies


bs[b] = slow_method(a, b)
end
end


  • Hash#deleteremoves a value from the hash and returns it. This is useful for stripping
    out keyword arguments from a hash before passing it along somewhere.


Kernel



  • Kernel#Array tries to coerce its argument into an array:
    Array([1,2,3]) # => [1, 2, 3]
    Array(1..3) # => [1, 2, 3]
    Array(1) # => [1]


Module



  • Module#remove_methodremoves a method from the specified class.Module#undef_method,
    on the other hand, actively prevents that method from being invoked on the class; it
    inserts a special entry into them_tbl that stops method lookup.


Proc



  • Proc#[] is shorthand forProc.call.
    p = lambda{|x| x * 2}
    p[3] # => 6


String



  • String#%(args)interpolates the arguments into itself in the manner ofsprintf. To pro-
    vide more than one value for interpolation, you must supply an array.
    "%.5f" % Math::PI # => "3.14159"


"%.5f, %.5f" % [Math::PI, Math::E] # => "3.14159, 2.71828"


  • String#returns the portion of the string that matches the given regular
    expression. If there is no matching portion,nil is returned.
    "asdf"[/sd/] # => "sd"
    "asdf"[/^sd/] # => nil
    "asdf"[/d(.)/,1] # => "f"

  • String#scan(regex)collects all of the regular expression’s matches against the string
    into an array. If the pattern has captures, each element of the array is itself an array of
    captured text.
    "asdf".scan(/[a-e]/) # => ["a", "d"]
    "hello ruby; hello regex".scan(/hello (\w+)/) # => [["ruby"], ["regex"]]

Free download pdf