- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Ruby 魔法 学习笔记之一由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、向对象显示的发送消息 我们可以向对象直接发送消息: Ruby代码 。
复制代码 代码如下
class HelloWorld def say(name) print "Hello, ", name end end hw = HelloWorld.new hw.send(:say,"world") 。
我们通常使用hw.say("world"),但send可以对private的方法起作用。 不光如此send可以使程序更加动态,下面我们看看一个例子: 我们定义了一个类Person,我们希望一个包含Person对象的数组能够按 照Person的任意成员数据来排序: Ruby代码 。
复制代码 代码如下
class Person attr_reader :name,:age,:height def initialize(name,age,height) @name,@age,@height = name,age,height end def inspect "#@name #@age #@height" end end 。
在ruby中任何一个类都可以随时打开的,这样可以写出像2.days_ago这样优美 的code,我们打开Array,并定义一个sort_by方法: Ruby代码 class Array def sort_by(sysm) self.sort{|x,y| x.send(sym) <=> y.send(sym)} end end 我们看看运行结果: Ruby代码 people = [] people << Person.new("Hansel",35,69) people << Person.new("Gretel",32,64) people << Person.new("Ted",36,68) people << Person.new("Alice", 33, 63) p1 = people.sort_by(:name) p2 = people.sort_by(:age) p3 = people.sort_by(:height) p p1 # [Alice 33 63, Gretel 32 64, Hansel 35 69, Ted 36 68] p p2 # [Gretel 32 64, Alice 33 63, Hansel 35 69, Ted 36 68] p p3 # [Alice 33 63, Gretel 32 64, Ted 36 68, Hansel 35 69] 这个结果是如何得到的呢? 其实除了send外还有一个地方应该注意attr_reader,attr_reader相当于定义了name, age,heigh三个方法,而Array里的sort方法只需要提供一个比较方法: x.send(sym) <=> y.send(sym) 通过send得到person的属性值,然后在使用<=>比较 2、定制一个object << object ruby不仅可以打开一个类,而且可以打开一个对象,给这个对象添加或定制功能,而不影响 其他对象: Ruby代码 a = "hello" b = "goodbye" def b.upcase gsub(/(.)(.)/)($1.upcase + $2) end puts a.upcase #HELLO puts b.upcase #GoOdBye 我们发现b.upcase方法被定制成我们自己的了 如果想给一个对象添加或定制多个功能,我们不想多个def b.method1 def b.method2这么做 我们可以有更模块化的方式: Ruby代码 b = "goodbye" class << b def upcase # create single method gsub(/(.)(.)/) { $1.upcase + $2 } end def upcase! gsub!(/(.)(.)/) { $1.upcase + $2 } end end puts b.upcase # GoOdBye puts b # goodbye b.upcase! puts b # GoOdBye 这个class被叫做singleton class,因为这个class是针对b这个对象的。 和设计模式singleton object类似,只会发生一次的东东我们叫singleton. << self 给你定义的class添加行为 Ruby代码 class TheClass class << self def hello puts "hello!" end end end TheClass.hello #hello! <<self修改了你定义class的class,这是个很有用的技术,他可以定义class级别 的helper方法,然后在这个class的其他的定义中使用。下面一个列子定义了访问 函数,我们希望访问的时候把成员数据都转化成string,我们可以通过这个技术来 定义一个Class-Level的方法accessor_string: Ruby代码 class MyClass class << self def accessor_string(*names) names.each do |name| class_eval <<-EOF def #{name} @#{name}.to_s end EOF end end end def initialize @a = [ 1, 2, 3 ] @b = Time.now end accessor_string :a, :b end o = MyClass.new puts o.a # 123 puts o.b # Fri Nov 21 09:50:51 +0800 2008 通过extend module给你的对象添加行为,module里面的方法变成了对象里面的 实例方法: Ruby代码 module Quantifier def any? self.each { |x| return true if yield x } false end def all? self.each { |x| return false if not yield x } true end end list = [1, 2, 3, 4, 5] list.extend(Quantifier) flag1 = list.any? {|x| x > 5 } # false flag2 = list.any? {|x| x >= 5 } # true flag3 = list.all? {|x| x <= 10 } # true flag4 = list.all? {|x| x % 2 == 0 } # false 3、创建一个可参数化的类: 如果我们要创建很多类,这些类只有类成员的初始值不同,我们很容易想起: Ruby代码 class IntelligentLife # Wrong way to do this! @@home_planet = nil def IntelligentLife.home_planet @@home_planet end def IntelligentLife.home_planet=(x) @@home_planet = x end #... end class Terran < IntelligentLife @@home_planet = "Earth" #... end class Martian < IntelligentLife @@home_planet = "Mars" #... end 这种方式是错误的,实际上Ruby中的类成员不仅在这个类中被所有对象共享, 实际上会被整个继承体系共享,所以我们调用Terran.home_planet,会输出 “Mars”,而我们期望的是Earth 一个可行的方法: 我们可以通过class_eval在运行时延迟求值来达到目标: Ruby代码 class IntelligentLife def IntelligentLife.home_planet class_eval("@@home_planet") end def IntelligentLife.home_planet=(x) class_eval("@@home_planet = #{x}") end #... end class Terran < IntelligentLife @@home_planet = "Earth" #... end class Martian < IntelligentLife @@home_planet = "Mars" #... end puts Terran.home_planet # Earth puts Martian.home_planet # Mars 最好的方法: 我们不使用类变量,而是使用类实例变量: Ruby代码 class IntelligentLife class << self attr_accessor :home_planet end #... end class Terran < IntelligentLife self.home_planet = "Earth" #... end class Martian < IntelligentLife self.home_planet = "Mars" #... end puts Terran.home_planet # Earth puts Martian.home_planet # Mars 4、Ruby中的Continuations: Continuations恐怕是Ruby中最难理解的概念了,它可以处理非局部的跳转, 它保存了返回地址和执行的环境,和c中的setjmp和longjump类似,但它保存 了更多的信息: axgle举的曹操的例子很形象,我们拿过来看看: 来自[http://www.javaeye.com/topic/44271] 曹操(caocao)被誉为“古代轻功最好的人 ”,是因为“说曹操,曹操到”这句名言。 在ruby中,曹操的这种轻功被称为callcc. Ruby代码 callcc{|caocao| for say in ["曹操","诸葛亮","周瑜"] caocao.call if say=="曹操" puts say #不会输出,因为曹操已经飞出去了 end }#“曹操”飞到这里来了(跳出了callcc的block,紧跟在这个block后面,继续执行下面的ruby代码) puts "到" callcc里的caocao是个"延续"(Continuation)对象.这个对象只有名叫“call"的这样一个方法。 当执行了caocao.call后,caocao就会飞到callcc的块(block)后面,让ruby继续执行其下面的代码。 我上面给出的是一个从块里头”飞“到块外面的例子;下面是Programming Ruby给出的从代码后面”飞“到代码前面的例子: Ruby代码 arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] callcc{|$cc|}#下面的$cc.call如果被执行,就会飞回到这里(callcc的块之后)。 puts(message = arr.shift) $cc.call unless message =~ /Max/ 例子大多来自<<The ruby way>> 。
最后此篇关于Ruby 魔法 学习笔记之一的文章就讲到这里了,如果你想了解更多关于Ruby 魔法 学习笔记之一的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
OkHttp的作用 OkHttp is an HTTP client。 如果是HTTP的方式想得到数据,就需要我们在页面上输入网址,如果网址没有问题,就有可能返回对应的String字符串,如果这个地址
Record 一个重要的字符串算法,这是第三次复习。 通过总结我认为之所以某个算法总是忘记,是因为大脑始终没有认可这种算法的逻辑(也就是脑回路)。 本篇主要讲解从KMP的应用场景,
SQL 注入基础 【若本文有问题请指正】 有回显 回显正常 基本步骤 1. 判断注入类型 数字型 or 字符型 数字型【示例】:
标签: #Prompt #LLM 创建时间:2023-04-28 17:05:45 链接: 课程(含JupyterNotebook) , 中文版 讲师: An
Swift是供iOS和OS X应用编程的新编程语言,基于C和Objective-C,而却没有C的一些兼容约束。Swift采用了安全的编程模式和添加现代的功能来是的编程更加简单、灵活和有趣。界面则基于
红日靶机(一)笔记 概述 域渗透靶机,可以练习对域渗透的一些知识,主要还是要熟悉 powershell 语法,powershell 往往比 cmd 的命令行更加强大,而很多渗透开源的脚本都是 po
八大绩效域详细解析 18.1 干系人绩效域 跟干系人所有相关的活动. 一、预期目标 ①与干系人建立高效的工作关系 ②干系人认同项目目标 ③支持项目的干系人提高
18.3 开发方法和生命周期绩效域 跟开发方法,项目交付节奏和生命周期相关的活动和职能. 一、预期目标: ①开发方法与项目可交付物相符合; ②将项目交付与干系人价值紧密
18.7 度量绩效域 度量绩效域涉及评估项目绩效和采取应对措施相关的活动和职能度量是评估项目绩效,并采取适当的应对措施,以保持最佳项目绩效的过程。 一、 预期目标: ①对项目状况
pygraphviz 安装,windows系统: 正确的安装姿势: Prebuilt-Binaries/PyGraphviz at master · CristiFati/Prebuilt-Binar
今天给大家介绍IDEA开发工具如何配置devtools热加载工具。 1、devtools原理介绍 spring-boot-devtools是spring为开发者提供的热加载
一 什么是正则表达式 // 正则表达式(regular expression)是一个描述字符模式的对象; // JS定义RegExp类表示正则表达式; // String和RegExp都定义了使用
目前是2022-04-25 23:48:03,此篇博文分享到互联网上估计是1-2个月后的事了,此时的OpenCV3最新版是3.4.16 这里前提是gcc,g++,cmake都需要安装好。 没安装好的,
一、概述 1、Flink 是什么 Apache Flink is a framework and distributed processing engine for stateful comput
一、window 概述 Flink 通常处理流式、无限数据集的计算引擎,窗口是一种把无限流式数据集切割成有限的数据集进行计算。window窗口在Flink中极其重要。 二、window 类型 w
一、触发器(Trigger) 1.1、案例一 利用global window + trigger 计算单词出现三次统计一次(有点像CountWindow) 某台虚拟机或者mac 终端输入:nc -
一、时间语义 在Flink 中涉及到三个重要时间概念:EventTime、IngestionTime、ProcessingTime。 1.1、EventTime EventTime 表示日志事
一、概述 以wordcount为例,为什么每次输入数据,flink都能统计每个单词的总数呢?我们都没有显示保存每个单词的状态值,但是每来一条数据,都能计算单词的总数。事实上,flink在底层维护了每
一、概述 checkpoint机制是Flink可靠性的基石,可以保证Flink集群在某个算子因为某些原因(如 异常退出)出现故障时,能够将整个应用流图的状态恢复到故障之前的某一状态,保 证应用流图状
一、standalone 部署模式 1、下载安装包 下载安装包地址 有两种安装包类型: 第一种是带 Hadoop依赖的(整合YARN) 第二种是不带 Hadoop依赖的(Standalone模式)
我是一名优秀的程序员,十分优秀!