一. 哈希变量(相当于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. template模板循环嵌套循环

    template嵌套循环写法:在第一次循环里面需要循环的地方再写个循环,把要循环的数据对象改为第一层的循环对象别名 //template模板循环嵌套循环 <script id="ban ...

  2. 深入使用Vue + TS

    深入使用TS 支持 render jsx 写法 这里一共分两步 首先得先让 vue 支持 jsx 写法 再让 vue 中的 ts 支持 jsx 写法 让 vue 支持 jsx 按照官方做法,安装Bab ...

  3. IDEA如何将写好的java类(UDF函数)打成jar包上传linux

    一.编写一个UDF函数,实现将字符串大写转小写 import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text; ...

  4. jdk git maven Jenkins的配置

    前言 搭建Jenkins的笔记. JDK 1.  jdk 下载地址 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...

  5. 编译内核时报错./include/net/sch_generic.h:535:28: error: inlining failed in call to always_inline 'qdisc_pkt_len': indirect function call with a yet undetermined callee static inline unsigned int qdisc_pkt_

    直接修改头文件include/net/sch_generic.h中的qdisc_pkt_len函数 将static inline unsigned int qdisc_pkt_len修改为: stat ...

  6. LC 660. Remove 9 【lock, hard】

    Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...

  7. 【React自制全家桶】三、React使用ref操作DOM与setState遇到的问题

    在React中同时使用ref操作DOM与setState常常会遇到 比如操作的DOM是setState更新之前的DOM内容,与想要的操作不一致.导致这样的原因是setState函数是异步函数. 就是当 ...

  8. LoaderDialog自定义加载框的实现

    package com.loaderman.loadingdialogdemo; import android.app.Dialog; import android.content.Context; ...

  9. WPF Visifire 图表控件

    Visifire WPF 图表控件 破解 可能用WPF生成过图表的开发人员都知道,WPF虽然本身的绘图能力强大,但如果每种图表都自己去实现一次的话可能工作量就大了, 尤其是在开发时间比较紧的情况下.这 ...

  10. 绝对好用Flash多文件大文件上传控件

    本实例采用的是Uploadify上传插件,.NET程序,源程序是从网上找的,但是有Bug,已经修改好,并标有部分注释.绝对好用,支持单文件.多文件上传,支持大文件上传,已经过多方面测试,保证好用. 以 ...