`
Hooopo
  • 浏览: 329649 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

notes

    博客分类:
  • API
阅读更多
---------------------------------------------------------- Array#compact
     array.compact     ->  an_array
------------------------------------------------------------------------
     Returns a copy of _self_ with all +nil+ elements removed.

        [ "a", nil, "b", nil, "c", nil ].compact
                          #=> [ "a", "b", "c" ]


irb default to auto complete:
irb(main):002:0> require'irb/completion'
=> true
irb(main):003:0> o = Object.new
=> #<Object:0x2ca4c8c>
irb(main):004:0> def o.ccc
irb(main):005:1> puts "ccc"
irb(main):006:1> end
=> nil
irb(main):007:0> o.c
o.ccc    o.class  o.clone
irb(main):007:0> o.c


get total days in a month

irb(main):019:0> d0 = Date.new(2009,12,1)
=> #<Date: 4910333/2,0,2299161>
irb(main):020:0> puts d0
2009-12-01
=> nil
irb(main):021:0> d1 = d0 >> 1
=> #<Date: 4910395/2,0,2299161>
irb(main):022:0> puts d1
2010-01-01
=> nil
irb(main):023:0> d2 = d1 - 1
=> #<Date: 4910393/2,0,2299161>
irb(main):024:0> puts d2
2009-12-31
=> nil
irb(main):025:0> d3 = d2.day
=> 31
irb(main):026:0> d4 = Date.new(2009,12,-1)
=> #<Date: 4910393/2,0,2299161>
irb(main):027:0> puts d4
2009-12-31
=> nil
irb(main):028:0> def days_of_month(year,month)
irb(main):029:1> (Date.new(year,12,-1) << 12 - month).day
irb(main):030:1> end
=> nil
irb(main):031:0> days_of_month(2004,2)
=> 29
irb(main):032:0> days_of_month(2003,2)
=> 28
irb(main):033:0>


sort array ascending except zero :
Hello, I need to sort
[1,4,2,0,8,9] to [1,2,4,8,9,0]
A simple ascending sort but with the zero values to the end
I really don't see how to do

1.override <=> on Fixnum(显然不是一个好的方法)
2.
a=[1,3,2,5,2,0,3,0,4,0,3]
max = a.max + 1
p a.sort_by{|x| x.zero? ? max : x}


3.
INF = 1.0 / 0.0
p a.sort_by {|x| x.zero? ? INF : x}

a.sort do |x,y| 
  case 
  when x == 0 then  1 
  when y == 0 then -1 
  else             
    x <=> y 
  end 
end


4.
tmp = a.partition{|x| x.nonzero? } 
p (tmp[0].sort + tmp[1])

5.
a.sort.sort_by{|n| n.zero? ? 1 : 0}

6.
tmp = a.sort.select{|x| x.nonzero? } 
p (tmp + Array.new((a.size - tmp.size),0))

7.delegate
require 'delegate'
require 'pp'

SFix = DelegateClass(Fixnum)

class SFix
  def <=> o
    num = __getobj__
    case
    when num == 0 then  1
    when o   == 0 then -1
    else num <=> o.__getobj__
    end
  end
end

# ======================================================

puts "Original Array"
x = [1,4,2,0,8,9]
# outputs:
# Original Array
# [1, 4, 2, 0, 8, 9]
pp x
puts

puts "Sorted Array"
# outputs:
# Sorted Array
# [1, 2, 4, 8, 9, 0]
pp x.sort_by {|i| SFix.new i}
puts


8.
require 'pp'


# You probably should put the following in it's own file
# and require it from here
# ======================================================
require 'delegate'

class CustomArray

  class MyFixnum < DelegateClass(Fixnum)

    def initialize(n)
      super(n)
    end

    def <=>(n)
      if self == 0
        return 1
      else
        if n == 0
          return -1
        else
          return  1 if self  > n
          return  0 if self == n
          return -1 if self  < n
        end
      end
    end

  end

  def initialize(my_array)
    @my_array = my_array.map{|n| MyFixnum.new(n)}
  end

  def sort
    return @my_array.sort.map{|n| n.to_i }
  end

end
# ======================================================

puts "Original Array"
x = [1,4,2,0,8,9]
# outputs:
# Original Array
# [1, 4, 2, 0, 8, 9]
pp x
puts

puts "Sorted Array"
# outputs:
# Sorted Array
# [1, 2, 4, 8, 9, 0]
pp CustomArray.new([1,4,2,0,8,9]).sort
puts



SimpleDelegate有什么用?
   require'delegate'
   class Stats
     def initialize
       @source = SimpleDelegator.new([])
     end
     
     def stats( records )
       @source.__setobj__(records)
       	
       "Elements:  #{@source.size}\n" +
       " Non-Nil:  #{@source.compact.size}\n" +
       "  Unique:  #{@source.uniq.size}\n"
     end
   end
   puts "SimpleDelegator".center(20,"-")
   s = Stats.new
   puts s.stats(%w{James Edward Gray II})
   puts
   puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
   
   class StatsNormal
     def initialize
       @source = []
     end
     
     def stats( records )
       @source = records
       	
       "Elements:  #{@source.size}\n" +
       " Non-Nil:  #{@source.compact.size}\n" +
       "  Unique:  #{@source.uniq.size}\n"
     end
   end
   puts "Normal".center(20,"-")
   s = StatsNormal.new
   puts s.stats(%w{James Edward Gray II})
   puts
   puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
   

待续..
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics