Skip to content

ProjectionOperators

surya edited this page Sep 28, 2011 · 1 revision

Projection operators

  • Simple 1

    numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ]
    output =  _{ 
      from n
      in_ numbers
      select n + 1
    }.to_a
    output.should == [6, 5, 2, 4, 10, 9, 7, 8, 3, 1
  • Simple 2

    Product = Struct.new(:name, :price)
    products = [Product.new("shoes", 1.75), Product.new("glasses", 55.55), Product.new("pencil", 5.20)]
    
    product_names = _{
      from p
      in_ products 
      select p.name
    }.to_a
    product_names.should == ["shoes","glasses",
  • Select - Transformation

    numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ]
    strings = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ]
    text_nums = _{
      from n
      in_ numbers
      select strings[n]
    }.to_a
    text_nums.should == ["five", "four", "one", "three", "nine", "eight", "six", "seven", "two", "zer
Clone this wiki locally