1月24日 ruby基础3部分 Numeric, Array已学。
<div style="background:lightblue">
第12章 数值类
12.1 数值的构成
Numeric-> Integer-> Fixnum,Bignum(非常大的整数)
-> Float
-> Rational (rational thoughts, decisions etc are based on reasons rather than emotions)
无限不循环小数以外的数(整数,分数)
-> Complex复数
#=> Rational(分子,分母) 分子:numerator,分母:denominator
Complex对象用"Complex(实数,虚数)"的形式定义 .Complex(real, imaginary)
12.2数值的字面量
0b1111011 #=>二进制整数123
123.45 浮点小数
1.23e4 #=> 12300
1.23e-4 #=> 0.000123
123_123 #=> 123123 下划线会被忽略,这样写看起来很舒服,用起来方便
123r #=> 有理数(123/1)
123i #=> 虚数的123i
12.3算数运算
% 取余运算, **乘方运算
整数和浮点数运算的结果是浮点数。
整数除以有理数的结果是有理数。
> r = (2/5r) + (1/3r) #=> (11/15)
除法:
x.div(y) 返回x除以y以后的商的整数
x.quo(y) 返回x除以y以后的商
x.remainder(y) 返回余数,结果的符号和x一样
x.modulo(y) #=> %
12.4 Math模块
提供了三角函数,对数函数等常用的函数运算法,和2个常量PI ,E
12.5数值类型转换
> 10.to_f
12.6 位运算
对以二进制表现的整数的每个二进制位进行的操作和运算。
bit(binary digit) 0或1,计算机中的最小数据单位。
1个字节有8bit. 1个字节可以表示十进制数0-255.十六进制00到FF
12.7随机数
- 没有规程和法制依据
- 一定范围内的数会均等的出现
Random.rand 得到随机数。
rand(100) #=> (0..100)指定正参数后返回0至正整数之间的数值0至99
属于伪随机数,是用算法生成的看起来相似随机数。但需要以某个值为基础,这个值叫做种子。
因此如果种子一样,得到的值也可能重复。
Random.new
随机生成一个种子。例子:
> r = Random.new
Ruby 有个securerandom库,用于信息安全领域用到的随机数字。
12.8 计数
n.times{|i| ...}
from.upto(to){|i|...}
from.downto(to){|i|...}
from.step(to, step){|i|...}
step(by: step, to: limit) {|i| block } → self
ary = 2.step(10).collect{|i| i*2 }
=> [4, 6, 8, 10, 12, 14, 16, 18, 20]
12.9 近似值误差。
浮点数的误差,原因:二进制无法正确的表示1/5, 1/3之类的无限数,会在适当位置截断,这样就产生了误差。
可以用Rational进行类似运算。
Comparable 模块
比较运算符也是方法,这个模块封装了比较运算符, 将其Mix-in到类后,就可以实现对实例进行比较的方法。
Numeric, String, Time都包含了Comparable模块。
13 Array
https://ruby-doc.org/core-2.5.0/Array.html#method-i-delete
13.2 创建
Array.new(长度,初始值) #=>new(size=0, default=nil) 可以用于创建value相同的数组。
new(array) #复制一个新数组。两者无关系
另外如果是以下写法则仅仅加个标签:
⚠️ 其中的区别:
new(size) {|index| block } #根据index索引,来创建每个值value.例子:
13.22 %w %i
%w:创建不包含空白的字符串数组
=> ["Ruby", "Per1", "Python", "Scheme", "Pike"]
%i : 创建元素符号数组
=> [:Ruby, :Per1, :Python, :Scheme]
13.23 to_a方法,每个k-v对儿都成为一个数组,统一放到一个大数组中。
13.24 使用String的split method
=> ["2018/2/1", " foo.html", " proxy.rb"]
13.3 index的使用方法
13.31 获得element
- a[n]
- a[n..m] 或者a[n...m]
- a[start, length]
=> ["c", "d", "e"]
13.32 (批量)替换element
a[index] = item
批量替换,可以使用上节的方法。 如 a[n..m] = []
13.33 插入元素,(替换0个元素)
a[n, 0] #在n前面插入元素。
13.34 values_at(n1, n2...) -> new_ary 通过index获得想要的element
13.4 作为集合的数组
ary = ary1 & ary2 交集
ary = ary1 | ary2 并集
ary = ary1 - ary2 差运算
例子:
| 和 + 的区别:
| 两个数组最后只保留唯一的元素,+ 后面的数组附加到前面的数组后 。
13.5 作为列的Array
push 和 << 类似,都是 添加到最后。
shift 删除第一个,pop删除最后一个
unshift 添加到第一个,。
=> ["a", "b", "c", "d", "e", "f"]
> alpha => ["b", "c", "d", "e", "f"]
13.6 Array 的 主要method
13.61 add element
- unshift(obj,...) -> ary
- ary << obj → ary
- push(obj, ... ) → ary 等同于<< ,但可以放多个object
- a.concat(b) -> ary concatenate:to link or join together, esp in a chain or series (数组和字符串都有的方法) concat(other_ary1, other_ary2,...)-> ary (ruby,2.5版本的新增功能)
- ary + other_ary → new_ary
- a[n] = item; 修改
- a[n..m] = item; 范围修改
- a[n, length] = item; 如果length等于0,相当于在n,前面插入item.
Freeze方法
> a = [1,2,3] => [1, 2, 3]
dup和clone的区别:
While clone
is used to duplicate an object, including its internal state, dup
typically uses the class of the descendant object to create the new instance. When using dup, any modules that the object has been extended with will not be copied.
13.62 从数组中删除元素
a.compact -> new_ary
Returns a copy of self
with all nil
elements removed.
a.compact! ->ary or nil
compact:(vt)to press sth together so that it becomes smaller or more solid.
delete:根据元素删除
a.delete(obj) -> item or nil
a.delete(obj){block} -> item or result of block(false/nil的话返回block)
Deletes all items from self
that are equal to obj
.If the optional code block is given, the result of the block is returned if the item is not found.
delete_at: 根据索引删除a.delete_at(index) -> obj or nil
delete_if / reject! / reject :用循环进行条件选删
a.delete_if{|item| block } -> ary
Delete every element of self for which block evaluates to true, 每次删除立即生效。而不是等循环迭代 iteration is over结束后再一起生效。
类似于:
a.reject! {|item| block } → ary or nil
reject {|item| block } → new_ary
slice
a.slice!(n)
a.slice!(n..m)
a.slice!(n, len)
根据index,删除a的指定部分,并返回删除部分的值。
uniq!
a.uniq -> new_ary
a.uniq{|item| ...} -> new_ary
a.uniq! -> ary
a.shift and a.pop
13.63 替换数组
collect, map:循环遍历
collect { |item| block } → new_ary
#把每次块的最后一行代码的结果集合成数组,然后return
collect等同于map,
collect!{|item| block } -> ary
fill
a.fill(value) ->ary
a.fill(value, begin [,length] ) # a[n,len] = item
a.fill(value, n..m) # a[n..m] = item
a.flatten(level) -> new_ary #把数组内的嵌套数组去掉,根据level去掉层数。
a.reverse -> new_ary
a.sort -> new_ary #二分快速排序法,排序。
a.sort{|a,b| block} -> new_ary
Comparisons for the sort will be done using the <=> operator or using an optional code block.
a.sort_by
13.7 数组和迭代器
数组是object的集合,iterator是循环处理的方法。
接受者为范围对象,而结果为数组对象,迭代器和数组被紧密的结合在一起了。
13.8处理Array的element
13.81循环和索引
for i in 0..n
block
end
13.82 each 和 each_with_index
13.83 while 可以用来逐个删除数组元素。
list =[1,2,3,4]
i = 0
while i < list.size
list.pop
i += 1
end
puts list #=> []
13.9 数组的元素
数组内可以嵌套。
初始化有两种定义:
- Array.new(size, default) #这样每次改一个数,所有内部对象都会改
- Array.new(size){|index| block} # 各个嵌套的块都是独立的。
> a = Array.new(3, [0,0,0])
Enumerable mode
The Enumerable mixin provides collection classes with several traversal and searching methods,and with the ability to sort. The class must provide a method each,which yields successive members of the collection. if Enumerable#max,#min,or #sort is used, the objects in the collection must also implement a meaningful <=> operator,as these methods rely on an ordering between members of the collection.
Public Instance Methods:
all?[{|obj| block}] -> true or false #反义词 none?
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil.
%w[ant bear cat].all? { |word| word.length >= 3 } #=> true
any?[{|obj| block}] -> true or false
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.
%w[ant bear cat].any? { |word| word.length >= 4 } #=> true
collect{|obj| block } ->new_array 把各元素的块执行结果以数组的形式返回。
就是对原数组对象进行修改后返回修改后的形式。
count ->int
count(item) ->int #If an argument is given, the number of items in enum that are equal to item are counted.
cycle(n = nil) {|obj| block} -> nil #根据n参数,决定遍历几遍对象。类似times方法。
Calls block for each element of enum repeatedly n times or forever if none or nil is given.
select {|obj| block } ->array #等同find_all 省略了if条件判断,反义词reject
Returns an array containing all elements of enum for which the given block return returns a true value.
加!号则是改变自身。
(1..10).find_all { |i| i % 3 == 0 } #=> [3, 6, 9]
each_slice(n) {...} -> nil
Iterates the given block for each slice of <n> elements.(英文)
每次把n个元素放到块中执行,直到遍历完所有元素。(我的翻译)
指定参数n,n个元素为一组,把各组元素传给块执行。(教程翻译)
(1..10).each_slice(3) { |a| p a }
=>
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
find #等同detect
to_a
# 使用范围对象的to_a方法
a = (1..100).to_a
inject(initial=nil){|memo, obj| block } ->obj
例子:
a = (1..10).to_a
a.inject{|sum, n| sum + n} -> 55
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
join(separator=$,) → str
[ "a", "b", "c" ].join #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Arial; color: #3d3d3d; -webkit-text-stroke: #3d3d3d}
span.s1 {font-kerning: none}
1月24日 ruby基础3部分 Numeric, Array已学。的更多相关文章
- 1月10日 ruby基础教程,查漏补缺; 2月22日 Exception补充
https://ruby-doc.org/core-2.5.0/Exception.html 1月20日练习完1,2章. 第一章 初探 ‘’单引号不执行转义符. \t 制表符.\n 换行符. p me ...
- 2017年5月24日 HTML 基础知识(二)
1 快捷方式:html:xt +tab 过渡XHTML html:xs+tab 严格XHTML !+tab html5的标签结构 2.Charset 编码 <meta charset ...
- 36.React基础介绍——2019年12月24日
2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...
- 北京Uber优步司机奖励政策(4月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(3月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(2月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(12月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 如何看待 SAE 在2014 年 3 月 24 日发生的的大面积宕机事故?
3 月 24 日晚间大约 23 点左右,新浪云 SAE 一处核心机柜掉电,导致 SAE 平台下大量应用无法正常访问,并在 10 小时后才陆续修复.这次事故暴露 SAE 的哪些缺陷?SAE 运维人员又是 ...
随机推荐
- Python: re.compile()
compile(pattern,flags=0) 1.编译一个正则表达式模式,返回一个模式对象 2.第二个参数flags是匹配模式,可以使用按位或‘|'表示同时生效,也可以在正则表达式字符串中指定 P ...
- Hive 复制分区表和数据
1. 非分区表: 复制表结构: create table new_table as select * from exists_table where 1=0; 复制表结构和数据: create tab ...
- python 操作 hbase
python 是万能的,当然也可以通过api去操作big database 的hbase了,python是通过thrift去访问操作hbase 以下是在centos7 上安装操作,前提是hbase已经 ...
- win10锁屏界面无法更新
win10的锁屏界面都是巨硬公司推送过来的,质量还不错,最近锁屏界面无法更新,解决方案如下: 以管理员身份运行cmd,分别运行如下两个命令 del /f /s /q /a "%userpro ...
- 第八篇:支持向量机 (SVM)分类器原理分析与基本应用
前言 支持向量机,也即SVM,号称分类算法,甚至机器学习界老大哥.其理论优美,发展相对完善,是非常受到推崇的算法. 本文将讲解的SVM基于一种最流行的实现 - 序列最小优化,也即SMO. 另外还将讲解 ...
- 20145309李昊《网络对抗技术》实验9 web安全基础实践
本实验在同学帮助下完成 一.实验准备 1.0 实验目标和内容 Web前端HTML.能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. Web前 ...
- IMAP协议命令(详细)
参照:http://www.cnblogs.com/qiubole/archive/2007/11/23/970180.html 转载:http://blog.sina.com.cn/s/blog_5 ...
- Python3基础 list 推导式 生成100以内的偶数列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- hdu 3698 UVA1490 Let the light guide us 线段树优化DP
题目链接 and 题目大意 hdu3698 但是 hdu的数据比较弱,所以在这luogu提交吧UVA1490 Let the light guide us 有一个\(n*m\)的平原,要求每行选一个点 ...
- HDU1143 (递推)题解
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...