早上git加星了webapp的教程

> h = Hash.new('Go Fishing')       => {}  // 创建一个空hash,设定"Go Fishing"为默认的值。 
> h['c']                                        => "Go Fishing" 

> 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_key?(key) → true or false

has_value?(value) → true or false

h = { "a" => 100, "b" => 200 }
h.has_key?("a") #=> true
h.value?(100)   #=> true 
h.value?(999)   #=> false




题目 23

给定一散列 Hash,输出有最大值(value)的键(key)

def find_max(hash)
  arr = hash.values
  arr_max = arr.max
  re_string = ''
  hash.each do |key, value|
    if value == arr_max
        re_string = re_string + key.to_s + " "
    end
  end
  return re_string
end
h = {
  "a" => 71,
  "b" => 38,
  "c" => 21,
  "d" => 80,
  "e" => 80
}
answer = find_max(h)
puts "有最大 value 的是 #{answer}" 

题目 26

# 给定一个数组包含 Hash,请过滤和排序
arr = [
  { "name" => "Peter", "age" => 30 },
  { "name" => "John", "age" => 15 },
  { "name" => "David", "age" => 45 },
  { "name" => "Steven", "age" => 22 },
  { "name" => "Vincent", "age" => 6 },
]
# 找出成年人,生成新的数组
adult = []
arr.each do |i|
  if i["age"] > 18  
    adult << i
  end
end
# 排序
result = adult.sort_by{|i| i["age"] }   //用到了sort_by{}

https://ruby-doc.org/core-2.4.1/Enumerable.html#method-i-sort_by

puts "所有成年人,并由小到大:#{result}"

1月4日编程基础hash的更多相关文章

  1. 2015年12月28日 Java基础系列(六)流

    2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流

  2. 1月10日 ruby基础教程,查漏补缺; 2月22日 Exception补充

    https://ruby-doc.org/core-2.5.0/Exception.html 1月20日练习完1,2章. 第一章 初探 ‘’单引号不执行转义符. \t 制表符.\n 换行符. p me ...

  3. 10月28日PHP基础知识测试题

    本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A) A:Windows系统 B:Apache服务器 C:MySQL数 ...

  4. 9月5日网页基础知识 通用标签、属性(body属性、路径、格式控制) 通用标签(有序列表、无序列表、常用标签)(补)

    网页基础知识 一.HTML语言 HTML语言翻译汉语为超文本标记语言. 二.网页的分类 1.静态页面:在静态页面中修改网页内容实际上就是修改网页原代码,不能从后台操作,数据来只能来源于原于代码.静态网 ...

  5. 2015年9月29日html基础加强学习笔记

    创建一个最简便的浏览器 首先打开VS2010,然后在空间里拖出一个Form控件当主页面,其次拖出一个Textbox控件作为地址栏,然后加一个Button控件作为按钮,最后拖出一个WebBrowser作 ...

  6. 2017年5月22日 HTML基础知识(一)

    一.Html 结构 1.1.HTML基本文档格式—<html> 标记 —<html>文档的头部好和主体内容 </html>  根标记 —<head> 文 ...

  7. 2017年10月21日 数据库基础&三大范式

    1. 数据库里面常用 int        整型nvarchar   字符串float       小数型decimal(,) 小数型money      小数型datetime   时间类型 ima ...

  8. 2015年11月26日 Java基础系列(六)正则表达式Regex

    package com.demo.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @autho ...

  9. 2015年11月26日 Java基础系列(五)异常Exception

    序,异常都是标准类Throwable的一些子类的对象. Throwable类的几个方法 1 getMessage() 返回描述该异常的信息 2 printStackTrace() 把消息和栈的跟踪记录 ...

随机推荐

  1. Linux命令:查看文件内容cat|tac|more|less|head|tail|nl|od

    查看文件内容的命令;cat, tac, more, less, head, tail, nl, 1)cat 由第一行开始显示文档内容,一直显示到最后 2)tac 从最后一行开始显示,一直显示到第一行内 ...

  2. Linux服务器---安装squid

    安装squid proxy就是软件代理或者代理服务器,而squid就是一种常用的proxy服务 1.安装squid [root@localhost wj]# rpm -qa | grep squid ...

  3. python如何序列化json数据

    使用json模块提供的loads方法和dumps方法,可以很方便的载入和读取json数据格式.而在具体实际应用中,我们使用python数据格式是 string.list 或dict等,这类格式如何直接 ...

  4. 推荐:Java性能优化系列集锦

    Java性能问题一直困扰着广大程序员,由于平台复杂性,要定位问题,找出其根源确实很难.随着10多年Java平台的改进以及新出现的多核多处理器,Java软件的性能和扩展性已经今非昔比了.现代JVM持续演 ...

  5. 护眼色的RGB值和颜色代码汇总

    源: 网上流行护眼色的RGB值和颜色代码汇总 网上流行护眼色的RGB值和颜色代码 在搜索引擎搜“护眼色”,就会搜出一堆关于保护眼睛的屏幕颜色文章,说的统统是一种颜色,有点像绿豆沙的颜色.方法就是在屏幕 ...

  6. Web负载均衡学习笔记之四层和七层负载均衡的区别

    0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...

  7. expect交互式创建账号密码

    这个脚本是我在建立samba用户的时候用到的,一开始我是一步一步的操作,后来嫌麻烦了,就写了这个脚本,也学习了一下expect. #!/usr/bin/expectset user [lindex $ ...

  8. 基础知识 - Golang 中的格式化输入输出

    ------------------------------------------------------------ [格式化输出] // 格式化输出:将 arg 列表中的 arg 转换为字符串输 ...

  9. SQL、SQL Server、MySQL与Oracle

    SQL (Structured Query Language),结构化查询语言,用来与多种数据库建立联系,根据ANSI(美国国家标准协会)的规定,SQL为RDBMS(关系型数据库)的标准语言. --- ...

  10. 20145204《网络对抗》逆向及bof基础实践

    20145204<网络对抗>逆向及bof基础实践 实践目的说明 实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...