一. 哈希变量(相当于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的字典, 简单的类型转换)的更多相关文章

  1. Ruby学习中(首探数组, 注释, 运算符, 三元表达式, 字符串)

    一. 数组 1.定义一个数组 games = ["英雄联盟", "绝地求生", "钢铁雄心"] puts games 2.数组的循环 gam ...

  2. PYTHON替代MATLAB在线性代数学习中的应用(使用Python辅助MIT 18.06 Linear Algebra学习)

    前言 MATLAB一向是理工科学生的必备神器,但随着中美贸易冲突的一再升级,禁售与禁用的阴云也持续笼罩在高等学院的头顶.也许我们都应当考虑更多的途径,来辅助我们的学习和研究工作. 虽然PYTHON和众 ...

  3. Python中什么是变量Python中定义字符串

    在Python中,变量的概念基本上和初中代数的方程变量是一致的. 例如,对于方程式 y=x*x ,x就是变量.当x=2时,计算结果是,当x=5时,计算结果是25. 只是在计算机程序中,变量不仅可以是数 ...

  4. 集成学习中的 stacking 以及python实现

    集成学习 Ensemble learning 中文名叫做集成学习,它并不是一个单独的机器学习算法,而是将很多的机器学习算法结合在一起,我们把组成集成学习的算法叫做“个体学习器”.在集成学习器当中,个体 ...

  5. Ruby学习中(条件判断, 循环, 异常处理)

    一. 条件判断 详情参看:https://www.runoob.com/ruby/ruby-decision.html 1.详情实例(看看就中了) #---------------# # LOL场均人 ...

  6. JAVA学习中Swing部分JDialog对话框窗体的简单学习

    package com.swing; import java.awt.Color;import java.awt.Container;import java.awt.event.ActionEvent ...

  7. Perl 变量:哈希变量

    Perl 哈希变量哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}. 1.创建哈希创建哈希可以通过以下两种方式: 1.为每个 key ...

  8. Ruby Rails学习中:Ruby内置的辅助方法,基础内容回顾补充

    一. Ruby内置的辅助方法 1.打开文件:app/views/layouts/application.html.erb(演示应用的网站布局) 来咱把注意力放在圈起来的那一行: 这行代码使用 Rail ...

  9. Python学习笔记(二)Python的数据类型和变量

    Python的字符串 Python使用''和""将字符串括起来,与ruby类似,特殊之处是Python可以使用r''表示''内部的字符串默认不转义,如: print(r'\\\t\ ...

随机推荐

  1. Linux-expect脚本-编写一个expect脚本

    1.声明expect #!/usr/bin/expect -f 2.设置超时时间,获取参数 set ip [lindex $argv 0 ] //接收第一个参数,并设置IP set password ...

  2. android 知识体系

  3. 最简SpringBoot程序制法

    JDK:1.8.0_212 IDE:STS4(Spring Tool Suit4 Version: 4.3.2.RELEASE) 工程下载:https://files.cnblogs.com/file ...

  4. yum源问题

    配置本地yum源 1.使用工具将iso文件上传到操作系统,或者直接挂载iso文件 2.配置yum #cd /etc/yum.repos.d/ 删除多余的repo文件 # vi /etc/yum.rep ...

  5. Mac下制作openwrt U盘启动盘

    华硕路由用腻了,正好家里有老旧淘汰的电脑,那么非常适合折腾一下OpenWrt,科学上网靠自己. 什么是OpenWrt:OpenWrt是适合于嵌入式设备的一个Linux发行版. 参考资料:https:/ ...

  6. html添加注释怎么弄?

    HTML 注释: <!--这是一段注释.注释不会在浏览器中显示.--> 这是一段普通的段落. 快捷键: 我用的是 Notpad++ 添加行注释 Ctrl+K 取消行注释 Ctrl+Shif ...

  7. [ASP.NET应用到的时间处理函数]

    第一种形式: System.DateTime.Now.ToString("D");         //2017年6月2日 System.DateTime.Now.ToString ...

  8. LC 650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  9. 软件-效率:Microsoft To Do

    ylbtech-软件-效率:Microsoft To Do Microsoft To Do To Do 让你从工作到娱乐都保持专注. 1.返回顶部 1. 智能每日计划 使用“我的一天”,用智能个性化建 ...

  10. 校验表单demo

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...