1月4日编程基础hash
早上git加星了webapp的教程
> h => {} //但并没有存入hash。仍然是空的。
h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
创建一个空hash,设置了默认的value值,如:
h["c"] #=> "Go Fish: c"
hash存入key,value对儿,不再为空。
hash的Methods中有 比较的operation. < , <= , ==, > ,>=.
clear -> hsh: Removes all key-value pairs from hsh.
values → array :
Returns a new array populated with the values from hsh. 同理 h.keys 回传一个数组包含所以keys.
h = { "a" => 100, "b" => 200, "c" => 300 }
h.keys #=> ['a', 'b', 'c']
h.values #=> [100, 200, 300]
另外: h.valuse_at('a', 'b') #=> [100, 200] 这是指定一组keys,回传一组【values】
Lenght -> integer
h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
h.length #=> 4
h.delete("a") #=> 200
h.length #=> 3
store(key, value) -> value
Associates the value given by value with the key given by key.
h = { "a" => 100, "b" => 200 }
h["a"] = 9
h["c"] = 4 //本来没有,于是新增一对kv值。
h #=> {"a"=>9, "b"=>200, "c"=>4}
h.store("d", 42) #=> 42
h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
merge(other_hash) → new_hash
merge(other_hash){|key, oldval, newval| block} → new_hash
Returns a new hash containing the contents of other_hash and the contents of hsh. If no block is specified, the value for entries with duplicate keys will be that of other_hash.
Otherwise the value for each duplicate key is determined by calling the block with the key, its value in hsh and its value in other_hash.
For example:
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| (newval - oldval)*2 }
#=> {"a"=>100, "b"=>108, "c"=>300}
h1 #=> {"a"=>100, "b"=>200}
has_value?(value) → true or false
h = { "a" => 100, "b" => 200 }
h.has_key?("a") #=> true
h.value?(100) #=> trueh.value?(999) #=> false
题目 23
给定一散列 Hash,输出有最大值(value)的键(key)
题目 26
https://ruby-doc.org/core-2.4.1/Enumerable.html#method-i-sort_by
1月4日编程基础hash的更多相关文章
- 2015年12月28日 Java基础系列(六)流
2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流
- 1月10日 ruby基础教程,查漏补缺; 2月22日 Exception补充
https://ruby-doc.org/core-2.5.0/Exception.html 1月20日练习完1,2章. 第一章 初探 ‘’单引号不执行转义符. \t 制表符.\n 换行符. p me ...
- 10月28日PHP基础知识测试题
本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A) A:Windows系统 B:Apache服务器 C:MySQL数 ...
- 9月5日网页基础知识 通用标签、属性(body属性、路径、格式控制) 通用标签(有序列表、无序列表、常用标签)(补)
网页基础知识 一.HTML语言 HTML语言翻译汉语为超文本标记语言. 二.网页的分类 1.静态页面:在静态页面中修改网页内容实际上就是修改网页原代码,不能从后台操作,数据来只能来源于原于代码.静态网 ...
- 2015年9月29日html基础加强学习笔记
创建一个最简便的浏览器 首先打开VS2010,然后在空间里拖出一个Form控件当主页面,其次拖出一个Textbox控件作为地址栏,然后加一个Button控件作为按钮,最后拖出一个WebBrowser作 ...
- 2017年5月22日 HTML基础知识(一)
一.Html 结构 1.1.HTML基本文档格式—<html> 标记 —<html>文档的头部好和主体内容 </html> 根标记 —<head> 文 ...
- 2017年10月21日 数据库基础&三大范式
1. 数据库里面常用 int 整型nvarchar 字符串float 小数型decimal(,) 小数型money 小数型datetime 时间类型 ima ...
- 2015年11月26日 Java基础系列(六)正则表达式Regex
package com.demo.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @autho ...
- 2015年11月26日 Java基础系列(五)异常Exception
序,异常都是标准类Throwable的一些子类的对象. Throwable类的几个方法 1 getMessage() 返回描述该异常的信息 2 printStackTrace() 把消息和栈的跟踪记录 ...
随机推荐
- 20154312 曾林 EXP9 Web安全基础
目录 -0.webgoat Could not find source file -1.基础问题回答 -2.环境配置 -3.Injection Flaws ----3.1.Numeric SQL In ...
- 服务器返回的json数据中含有null的处理方法
个人博客:http://guohuaden.com/2017/03/06/json-dataNull/因为有遇到过类似情况,所以就想到了一些解决方法,并且实践了一下,这里简单的做个记录. 注:有看到不 ...
- 页面点击,不是a标签也会刷新原因
页面点击,不是a标签也会刷新原因 点击事件冒泡,触发了a链接导致整个页面刷新了.直接阻止 事件冒泡即可 例子: $("tr .am-text-danger").click(func ...
- SpringBoot集成Socket服务后打包(war包)启动时如何启动Socket服务(web应用外部tomcat启动)
1.首先知道SpringBoot打包为jar和war包是不一样的(只讨论SpringBoot环境下web应用打包) 1.1.jar和war包的打开方式不一样,虽然都依赖java环境,但是j ...
- java replaceAll之$替换
最近,在开发一个伪代码翻译工具的过程中,遇到个问题,我们在伪代码中使用spring EL的规则,将非变量限定在${"1"}中,解析后根据上下文的类型转换为相应的java类型.在规则 ...
- 写给java程序员的c++与java实现的一些重要细微差别-附完整版pdf学习手册
0.其实常规的逻辑判断结构.工具类.文件读写.控制台读写这些的关系都不大,熟悉之后,这些都是灵活运用的问题. 学习c/c++需要预先知道的一个前提就是,虽然有ANSI C标准,但是每个c/c++编译器 ...
- nodejs 导出 exel文件 xlsx
参考: https://www.npmjs.com/package/node-xlsx Building a xlsx import xlsx from 'node-xlsx'; // Or var ...
- 20145312 《网络对抗》 Web安全基础实践
20145312 <网络对抗> Web安全基础实践 问题回答 SQL注入攻击原理,如何防御 原理:攻击者在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,把SQL语句当做 ...
- [c/c++]指针(3)
在指针2中提到了怎么用指针申配内存,但是,指针申配的内存不会无缘无故地 被收回.很多poj上的题都是有多组数据,每次地数组大小会不同,所以要重新申请 一块内存.但是原来的内存却不会被收回,也是说2.3 ...
- Python数据分析入门之pandas基础总结
Pandas--"大熊猫"基础 Series Series: pandas的长枪(数据表中的一列或一行,观测向量,一维数组...) Series1 = pd.Series(np.r ...