Ruby学习中(哈希变量/python的字典, 简单的类型转换)
一. 哈希变量(相当于Python中的字典)
详情参看:https://www.runoob.com/ruby/ruby-hash.html
1.值得注意的
(1). 创建Hash时需注意
# 创建一个空的Hash
months = Hash.new
puts months
print(months[1]) # 创建一个具有默认值得Hash
months = Hash.new( "month" )
# 或
months = Hash.new "month"
puts months
print(months[1]) 输出结果:
{}
报错 {}
month
(2).Ruby创建一个有数据的Hash时与Python创建一个有数据的dict时的区别
Python:
a = dict(a=1, b=2) # 正确
print(a)
b = dict[a=1, b=2] # 错误
print(b)
c = {["a", "b"]: 1} # 错误
print(c)
Ruby:
a = Hash(a=1, b=2) # 错误
puts a
a = Hash["a" => 1, "b" => 2] # 正确
puts a
b = Hash("a" => 1, "b" => 2) # 正确
puts b
c = Hash("a": 1, "b": 2) # 正确
puts c
d = Hash([1, "he"] => "hai") # 正确
puts d 输出结果:
error
{"a"=>1, "b"=>2}
{"a"=>1, "b"=>2}
{:a=>1, :b=>2}
{[1, "he"]=>"hai"}
(3).Ruby调用hash中的数据与Python调用dict中的数据时的区别
Python:
a = {"a": 1, "b": 2}
print(a["a"])
Ruby:
game = {"疾风剑豪" => "亚索", "流影之主" => "劫", "刀锋之影" => "泰隆"}
puts game
puts game["疾风剑豪"]
user = {name: "进不去啊", age: 18, gender: "男"}
puts user
puts user["name"] # nil
puts user[name] # 报错
puts user[:name] 输出结果:
{"疾风剑豪"=>"亚索", "流影之主"=>"劫", "刀锋之影"=>"泰隆"}
亚索
{:name=>"进不去啊", :age=>18, :gender=>"男"} error
进不去啊
注:Ruby关于字典中的方法大体与Python类似,请放心使用
二. 简单的类型转换
str = ""
puts str
str1 = str.to_i # 转整型
puts str1
str2 = str1.to_s # 转字符串
puts str2
str3 = str1.to_f # 转浮点
puts str3
注:这些转换方法与Python有很大的不同
str = "12345hei"
str1 = str.to_i
str2 = str.to_f
puts str1, str2
puts str1.class, str2.class 输出结果:
12345
12345.0
Integer
Float
str = "hei12345hei"
str1 = str.to_i
str2 = str.to_f
puts str1, str2
puts str1.class, str2.class 输出结果:
0
0.0
Integer
Float
str = "12345hei6789"
str1 = str.to_i
str2 = str.to_f
puts str1, str2
puts str1.class, str2.class 输出结果:
12345
12345.0
Integer
Float
str = "hei12345hei6789"
str1 = str.to_i
str2 = str.to_f
puts str1, str2
puts str1.class, str2.class 输出结果:
0
0.0
Integer
Float
注:经过to_i, to_f转换的字符串如果没有对应的值就会输出0或0.0,并且只会去字符串从首字符向后的所有的连续的数字,有且只取一次
三. 类(class)的再深入
详情参看:https://www.runoob.com/ruby/ruby-class.html
1.值得注意的
(1).Ruby类中的变量

注:Ruby中的类变量看的我有点懵逼(介是嘛呀),所以不推荐使用(别问为什么)
(2).静态类方法
Python:
class Foo(object):
def foo1(self):
print("") @staticmethod
def foo2():
print("") foo = Foo()
Foo.foo2() # 正确
foo.foo2() # 正确
Ruby:
class Game
def initialize(id, title, price) # 构造方法
@id = id
@title = title
@price = price
end def show_game
puts @id + " " + @title + " " + @price
end def self.to_str
puts "I Love This Game"
end
end one = Game.new("one", "LOL", "")
one.show_game
one.to_str # 错误 Game.to_str
Game::to_str 输出结果:
one LOL 0
error
I Love This Game
I Love This Game
注:类的静态方法,直属此类,不能被其它类所引用调用(具体稍后解释)
(3).Ruby 类的继承
class Game
def initialize(id, title, price)
@id = id
@title = title
@price = price
end def show_game
puts @id + " " + @title + " " + @price
end def self.to_str
puts "I Love This Game"
end
end class SteamGame < Game # 关于继承,与Python最大的区别
def steam_info
puts "G胖无敌"
end
end SteamGame.to_str my_game = SteamGame.new("new", "城市:天际线", "")
my_game.show_game
my_game.steam_info 输出结果:
I Love This Game
new 城市:天际线 100
G胖无敌
四. Ruby中的模块
详情参见:https://www.runoob.com/ruby/ruby-module.html
注:Python中也有模块这个概念,但和Ruby中模块的概念不相同
Python:
Python中的模块是以文件.py,且包含了 Python 对象定义和Python语句 Ruby:
模块(Module)是一种把方法、类和常量组合在一起的方式,具体就像是写一个类,只不过把class改为module(简单理解),和Python最大的区别就是Python是以文件作为区分,Ruby是以module作为区分
1.Ruby与Python的最大区别
Ruby中没有多继承!!!
but...
Ruby通过module实现了与多继承相同的思路
module BaseFunc
Version = "0.1.1" def v
return Version
end def add(a, b)
return a + b
end def self.show_version
return Version
end # 讲v方法定义范围静态方法
module_function :v
end puts BaseFunc::Version
puts BaseFunc.show_version
puts BaseFunc::show_version
puts BaseFunc.v
# puts BaseFunc.add(20 + 30) # 错误 class BaseClass
include BaseFunc
end puts "++++++++++++++++++++"
# puts BaseClass.show_version # 错误
# puts BaseClass.v # 错误
puts BaseClass::Version
my_cls = BaseClass.new
puts my_cls.add(20, 30)
# puts my_cls.show_version # 错误
module A
def a1
end
def a2
end
end
module B
def b1
end
def b2
end
end class Sample
include A
include B
def s1
end
end samp=Sample.new
samp.a1
samp.a2
samp.b1
samp.b2
samp.s1
Ruby多继承实例
2.Ruby关于模块的引用
(1).文件中引用
Ruby require 语句
语法:require filename
注:相当于Python中的import引入模块
实例:
require 'trig.rb'
require 'moral'
注:可以引入文件.rb或直接引入模块中的方法
(2).类中引用

。。。
未完待续
Ruby学习中(哈希变量/python的字典, 简单的类型转换)的更多相关文章
- Ruby学习中(首探数组, 注释, 运算符, 三元表达式, 字符串)
一. 数组 1.定义一个数组 games = ["英雄联盟", "绝地求生", "钢铁雄心"] puts games 2.数组的循环 gam ...
- PYTHON替代MATLAB在线性代数学习中的应用(使用Python辅助MIT 18.06 Linear Algebra学习)
前言 MATLAB一向是理工科学生的必备神器,但随着中美贸易冲突的一再升级,禁售与禁用的阴云也持续笼罩在高等学院的头顶.也许我们都应当考虑更多的途径,来辅助我们的学习和研究工作. 虽然PYTHON和众 ...
- Python中什么是变量Python中定义字符串
在Python中,变量的概念基本上和初中代数的方程变量是一致的. 例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是,当x=5时,计算结果是25. 只是在计算机程序中,变量不仅可以是数 ...
- 集成学习中的 stacking 以及python实现
集成学习 Ensemble learning 中文名叫做集成学习,它并不是一个单独的机器学习算法,而是将很多的机器学习算法结合在一起,我们把组成集成学习的算法叫做“个体学习器”.在集成学习器当中,个体 ...
- Ruby学习中(条件判断, 循环, 异常处理)
一. 条件判断 详情参看:https://www.runoob.com/ruby/ruby-decision.html 1.详情实例(看看就中了) #---------------# # LOL场均人 ...
- JAVA学习中Swing部分JDialog对话框窗体的简单学习
package com.swing; import java.awt.Color;import java.awt.Container;import java.awt.event.ActionEvent ...
- Perl 变量:哈希变量
Perl 哈希变量哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}. 1.创建哈希创建哈希可以通过以下两种方式: 1.为每个 key ...
- Ruby Rails学习中:Ruby内置的辅助方法,基础内容回顾补充
一. Ruby内置的辅助方法 1.打开文件:app/views/layouts/application.html.erb(演示应用的网站布局) 来咱把注意力放在圈起来的那一行: 这行代码使用 Rail ...
- Python学习笔记(二)Python的数据类型和变量
Python的字符串 Python使用''和""将字符串括起来,与ruby类似,特殊之处是Python可以使用r''表示''内部的字符串默认不转义,如: print(r'\\\t\ ...
随机推荐
- Mysql模拟故障恢复案例过程
一.数据库全备,全备脚本如下: [root@leader script]# cat bak_all.sh #!/bin/bash#Date: 2019-12-08#Author: chan#Mail: ...
- docker操作笔记
1.查看docker版本:docker info /docker version2.使用 docker run 命令来在容器内运行一个应用程序.如输出helloworld:docker run ub ...
- js对象之间的"继承"的五种方法
今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...
- C++ 编程风格指南
C++ Programming Style Guidelines 他人翻译中文版:click 让程序具有好的可读性 “避免日后 有人(或者自己)指着你的代码骂娘:这特么谁写的破烂 玩意”(来自:知乎- ...
- ActiveXObject常用方法
function getusername() { var WshNetwork = new ActiveXObject("WScript.Network"); alert(&quo ...
- 忘记Linux 3.X/4.x/5.x 宝塔面板密码的解决方案
进入ssh 输入以下命令重置密码(把命令最后面的 “testpasswd” 替换成你要改的新密码)注:若是debian/ubuntu用户,请使用有root权限的账户去执行这条命令 cd /www ...
- koa 项目实战(五)全球公用头像的使用
1.安装模块 npm install gravatar --save 2.使用 根目录/routes/api/users.js const gravatar = require('gravatar') ...
- maven 打包异常
异常信息: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE ...
- AXIS 1.4 自定义序列化/反序列化类
axis1.4的CalendarDeserializer 使用的时区是GMT,导致日期显示不准确 private static SimpleDateFormat zulu = new SimpleDa ...
- springboot2.0---控制台打印Mybatis的SQL记录
题记:每次使用mybatis出错,都不知道sql原因,debug也不出结果,索性将其打印出来,更加容易排错. 亲测有效,只需要将下面的logback.xml放置在resource目录下即可打印. 方式 ...