Archive for the ‘snippets’ Category

Snippet: remove .svn directories

find . -name ".svn" -exec rm -rf {} \;

via Mongrel list — Zed Shaw

One not documented behavior im lambda

proc { |…| block } => a_proc, lambda { |…| block } => a_procEquivalent to Proc.new, except the resulting Proc objects check the number of parameters passed when called.
Ruby official doc

But at least are one more difference :

def foo
  f = Proc.new { return "return from foo from inside proc" }

  f.call # control leaves foo here

  return "return from foo"

end

def bar

  f = lambda { return "return from lambda" }

  f.call # control does not leave bar here

  return "return from bar"

end

puts foo # prints "return from foo from inside proc"

puts bar # prints "return from bar"

Wikipedia

Snippet : session expires in rails

  def check_auth
    if session[:admin_authenticated] 
      if session[:expires]  "login"
      end
    else
      redirect_to :action => "login"
    end
  end

snippet : A benchmark in ruby

An easy snippets thats show how to do a benchmark :

require 'benchmark'

require 'base64'Benchmark.bm(8) do |x|

  n   = 1_000_000

  hsh = { :id => 1, :name => 'Foo', :description => 'bar',

          :created_at => '2007-06-01 16:05:41.592507',

          :updated_at => '2007-06-01 16:05:41.592507', }

  str = Base64::encode64(Marshal.dump(hsh))

  x.report('delete!:') { n.times { str.dup.delete!("n") << "n" } }

  x.report('gsub!  :') { n.times { str.dup.gsub!(/n(?=.)/, '') } }

  x.report('linear :') { n.times { str = str.dup; str.gsub!(/n(?=.)/, '') } }

  x.report('scan   :') { n.times { str.dup.scan(%r|.{1,60}|).join } }

end

via pastie.

Curried Function and labeled arguments in Ocaml

In mathematics, computer science and linguistics (semantics), currying or Schönfinkelisation[1] is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry). The technique was named by Christopher Strachey after logician Haskell Curry, though it was invented by Moses Schönfinkel and Gottlob Frege.

Wikipedia – curried function.


# let op f x y = (f) x y;;
val op : ('a -> 'b -> 'c) -> 'a -> 'b -> 'c = 

# let add = op (+);;
val add : int -> int -> int =

# let minus = op (-);;
val minus : int -> int -> int =

# let inc = add 1;;
val inc : int -> int 

When you carried a function, you must carried the next not curried argument, but you can swap the arguments or use labeled arguments.


# let flip f x y = f y x;;
val flip : ('a -> 'b -> 'c) -> 'b -> 'a -> 'c = 

# let dec = flip minus 1;;
val dec : int -> int =

# inc 5;;
- : int = 6

# dec 5;;
- : int = 4

# inc (dec 5);;
- : int = 5

The above code swap the arguments, the code belows use labeled arguments.


#  let op2 ~f ~x ~y = (f) ~x ~y;;
val op2 : f:(x:'a -> y:'b -> 'c) -> x:'a -> y:'b -> 'c = 

# let minus2 = op (-);;
val minus2 : int -> int -> int =

# let minus2 = op2 (-);;
val minus2 :  f:(x:'a -> y:'b -> (int -> int -> int) -> 'c) -> x:'a -> y:'b -> 'c =

# let dec2 = minus2 ~y:1;;
val dec2 : f:(x:'a -> y:int -> (int -> int -> int) -> 'b) -> x:'a -> 'b =

# dec 2;;
- : int = 1

Also we can use high order function to curried another function and reuse our code.


# let rec iter f list = match list with
     | [] -> []
     | head::tail -> f head :: iter f tail;;
val iter : ('a -> 'b) -> 'a list -> 'b list = 

# let inc = (+) 1;;
val inc : int -> int =

# let dec x = x-1;;
val dec : int -> int =

# let double x = x * 2;;
val double : int -> int =

# let double_each = iter double;;
val double_each : int list -> int list =

# let inc_each = iter inc;;
val inc_each : int list -> int list =

# let dec_each = iter dec;;
val dec_each : int list -> int list =

# double_each [1;2;3];;
- : int list = [2; 4; 6]

# inc_each [1;2;3];;
- : int list = [2; 3; 4]

# dec_each [1;2;3];;
- : int list = [0; 1; 2]

Ruby snippet : Remove duplicates in an array

module Enumerable

  def dups

    inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys

  end

end 


arr = %w{foo bar baz bar baz qux foo zub}

puts arr.dups.inspect # => ["baz", "foo", "bar"] 
Follow

Get every new post delivered to your Inbox.