---------------------------------------------------------- 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])
待续..
分享到:
相关推荐
在不包含Sticky Notes 便签的操作系统中手动安装Sticky Notes 便签,windows server2016测试通过;包含x86及x64; 方法:1、新建 C:\Program Files\Sticky Notes 2、拷贝 en-US、slc.dll、StickyNotes.exe 至 C:\...
Notes.jar Notes.jar Notes.jar
本书全面介绍了Lotus Notes R5中文版的操作和使用。共分24学时进行讲解,是自学和培训的优秀教程。它既适合初学者学习,也可以作为高级操作人员的参考书。 本书主要包括以下方面的内容:创建和发送电子邮件;使用...
NOTES分类视图的生成NOTES分类视图的生成
Goodnotes/Notability手帐模版万年历。共652页
notes2 notes2notes2 notes2notes2 notes2v
SideNotes是一个用于记笔记的智能应用程序。它在显示器的侧面显示和隐藏,以管理您的笔记而不会分散您的注意力。您最终可以使笔记井井有条,个性化,并且随时可用。简单的软件可提高您的生产力。 我们刚刚发布了...
Lotus Notes 自定义信头方法
教程名称: Lotus Notes使用详细教学视频(10讲)【】一:在notes的环境中工作【】七:使用文档格式化工具美化Notes Mail【】三:使用链接与附件【】九:使用日历视图与管理个人日历【】二:创建与发送邮件【】五:...
本系统演示了一套基于Notes平台的开源文档工作流。工作流的名称、节点和操作等配置保存在一组文档中,方便在运行时创建和修改。工作流可运行于Notes客户端或浏览器(需XPages支持)。 功能(features)包括: 工作流...
对于远程使用,选择“直接拨号”或“Notes 网络拨号”。 对于支持多种连接的场所,选择“定制”。 对于不连接的场所,选择“无连接”。 不要使用“Notes 直接拨号”。 注释 Notes 根据用户选择的场所显示不同的选项...
2024cfa一级考纲变化巨大,尽早复习notes意义重大!!
2024年frm一级notes免费分享给大家,抓紧时间下载,另外送最新的公示表哦
C:\work\hisi\Hisi\doc\application notes>dir 驱动器 C 中的卷没有标签。 卷的序列号是 8C68-6106 C:\work\hisi\Hisi\doc\application notes 的目录 2012-04-07 14:53 <DIR> . 2012-04-07 14:53 <DIR> .. 2011...
lotus domino notes安装文档
详细介绍了notes 8.5都带来了哪些新功能,特别是inotes,手机邮件等等
如何改变LotusNotes 工作区字体的大小
Notes客户端在第一次使用时,需要配置端口,有时配置完成端口重启Notes后,切换场所,扔旧不能访问服务器,文档描述了出现这个现象的可能原因及解决方法,详见文档。
新的cfa三级notes,5本还有quicksheet,欢迎下载!!!!
Notes模板,适合于notes初学者!