Ruby中的|XXX|意义

Python010

Ruby中的|XXX|意义,第1张

在 ruby 中,{|args| statements} 叫做 匿名函数,就是没有名字的小型函数。然后 each_byte 是一个方法,它接收一个可调用对象

有时候左花括号可以用 do 代替,有花括号用 end 代替,就是

do |args|

statements

end

这种形式。

9马巾帅分享: in in in 季节, on on on 星期。    

E.g, on a warm spring,on a hot summer, on a cool autumn, on a cold winter,

 in the early morning, in the late evening.

At Christmas(耶稣降生的时间点), at night, at noon, at midnight,

10许梅云:it同类同个,one同类不同个,that用语比较级代替不可数名词:The weather in China is hotter than that in Russia.

11张莉平:It took sb stm to do sth. A quarter, three quarters. One hour and a half, one and a half hours. GDP: Gross Domestic Product. Vegetables or fruit. Apple俩音节一个元音a一个辅音L. That’s all right.= It doesn’t matter./You are welcome.

12王蓓蓓:零冠词。

13许梅云:if,(如果,是否)(条件,状从)区分(时态,主将从现,作know宾语)

I don’t know if that man can save me. I didn’t know if that man

If you save me, I will marry you.(marry嫁娶都可以)

14张丽萍:直去双改:双尾 :停止聊天单脚跳,计划旅行掉下去。 Stop, chat, hop, plan, travel/trip, drop, nod.

独自方,高乐序,惯家形。零冠词:一是名称和复数。二是学科和语言,三是星期,季节,节日。

8 30

15爱科:固定搭配:

 His boss made him do much hard work.

He was made to do much hard work by his boss.

Mummy, I want you to buy a toy car for me.

Dear/Darling, I would like to see a film. Would you like to go with me?

16艳红只用(whether):He has no idea whether he comes or not.同位语从句

  The question is whether the enemy is marching towards us.表语从句

  Weather he comes or not has nothing to do with me.主语从句

17doing---动名词或者现在分词

 What’s your hobby?-- Collecting stamps is my hobby.

非谓语分类:to do, doing=现在分词+过去分词, done

18张艺田:祈使句:Do speak loudly.

19高琪: V爸唠叨你胖。V:动原  B:be+形  L:Let’s go. D:Don’t do it. N:No smoking. P:Please do me a favor. Let us go to the zoo. Don’t let us go to the zoo. Let’s 让我们一群人,大于三,let us 就你和我 不包括他。

插曲:转正答辩 :

At 6:30 on the morning of August 7th,2008.

+讲课总分总

+印象最深刻的一件事情·

+上次答辩没过的原因是什么

+对于这些点你在下面又做了一些什么工作

+这次答辩准备了多久+

20李海迪:e饿:一只长(wear)着头发的熊在哪里,在那里,在椅子上吃梨 

          oo:大脚厨师站在教室的羊毛木头上边吃曲奇边看好书。

21.Winnie王晶:情态动词+动原不受时态和单三影响。Ruby点评:学情介绍,总分总,为什么讲,不管对错让学生讲为什么。

22.小花:动词单三,直接加,以吃屎是错的结尾加es,以辅音+y结尾的:try,cry,carry, 以辅音+o结尾:do,go, Ruby:什么是辅音字母。

23.艳红:一般疑问句:be,情,助,总分总。什么是什么,对比一下,是一样的。先练习一道或者两道,I want to answer the call of nature. I want to do some make

up. I want to go to John.

24.May you be happy every day.可能,允许, 可能性:must/can/could/may/might。 小猪pk佩奇,,,.选几个讲清楚即可。

25.忆田:助动词。我的主要讲清楚讲几项东西。

26.张grace:都是四条,过去式:直去双改,去哑巴e,双尾字母。Stop,drop,plan,nod,beg,chat,trip.112—55—30.停止聊天单脚跳,计划点头掉下去。

27.ruby.非谓语:1.三种基本形式:to do(表目的,表将来), doing(表主动,表进行), done(表被动,表完成)2.用法:在一个简单句子中,当句中已有一个谓语动词,接着再出现一个动词时,需要用非谓语形式。例如:一个漂亮的小女孩坐在树下看书。A beautiful little girl is sitting under a tree reading a book.基础类的学生讲to do/doing的搭配。备注:所有的非谓语都可以用主语从句或定语从句来表达。Given考虑到/Having broken the cup,he feels sorry.

在Ruby中,有多种方法可以实现方法的动态调用。

1.

使用send方法

第一种实现动态方法调用是使用send方法,send方法在Object类中定义,方法的第一个参数是一个符号用来表示所要调用的方法,后面则是所调用方法需要的参数。

“This

is

a

dog1″.send(:length)

=>

14

上面的代码中通过send方法去对一个字符串执行length操作,返回字符串的长度。

class

TestClass

def

hello(*args)

”Hello

+

args.join(‘

‘)

end

end

a

=

TestClass.new

puts

a.send

:hello,

“This”,

“is”,

“a”,

“dog!”

执行结果为:

Hello

This

is

a

dog!

2.

使用Method类和UnboundMethod类

另一种实现动态方法调用是使用Object类的method方法,这个方法返回一个Method类的对象。我们可以使用call方法来执行方法调用。

test1

=

“This

is

a

dog1″.method(:length)

test1.call

=>

14

class

Test

def

initialize(var)

@var

=

var

end

def

hello()

”Hello,

@var

=

#{@var}”

end

end

k

=

Test.new(10)

m

=

k.method(:hello)

m.call

#=>

“Hello,

@iv

=

99″

l

=

Test.new(‘Grant’)

m

=

l.method(“hello”)

m.call

#=>

“Hello,

@iv

=

Fred”

可以在使用对象的任何地方使用method对象,当调用call方法时,参数所指明的方法会被执行,这种行为有些像C语言中的函数指针。你也可以把method对象作为一个迭代器使用。

def

square(a)

a*a

end

mObj

=

method(:square)

[1,

2,

3,

4].collect(&mObj)

=>

[1

4

9

16]

Method对象都是和某一特定对象绑定的,也就是说你需要通过某一对象使用Method对象。你也可以通过UnboundMethod类创建对象,然后再把它绑定到某个具体的对象中。如果UnboundMethod对象调用时尚未绑定,则会引发异常。

class

Double

def

get_value

2

*

@side

end

def

initialize(side)

@side

=

side

end

end

a

=

Double.instance_method(:get_value)

#返回一个UnboundMethod对象

s

=

Double.new(50)

b

=

a.bind(s)

puts

b.call

执行结果为:

100

看下面一个更具体的例子:

class

CommandInterpreter

def

do_2()

print

“This

is

2

end

def

do_1()

print

“This

is

1

end

def

do_4()

print

“This

is

4

end

def

do_3()

print

“This

is

3

end

Dispatcher

=

{

?2

=>

instance_method(:do_2),

?1

=>

instance_method(:do_1),

?4

=>

instance_method(:do_4),

?3

=>

instance_method(:do_3)

}

def

interpret(string)

string.each_byte

{|i|

Dispatcher[i].bind(self).call

}

end

end

interpreter

=

CommandInterpreter.new

interpreter.interpret(’1234′)

执行结果为:

This

is

1

This

is

2

This

is

3

This

is

4

3.

使用eval方法

我们还可以使用eval方法实现方法动态调用。eval方法在Kernel模块中定义,有多种变体如class_eval,module_eval,instance_eval等。Eval方法将分析其后的字符串参数并把这个字符串参数作为Ruby代码执行。

str

=

“Hello”

eval

“str

+

World!’”

=>

Hello

World!

sentence

=

%q{“This

is

a

test!”.length}

eval

sentence

=>

15

当我们在使用eval方法时,我们可以通过eval方法的第二个参数指明eval所运行代码的上下文环境,这个参数可以是Binding类对象或Proc类对象。Binding类封装了代码在某一环境运行的上下文,可以供以后使用。

class

BindingTest

def

initialize(n)

@value

=

n

end

def

getBinding

return

binding()

#使用Kernel#binding方法返回一个Binding对象

end

end

obj1

=

BindingTest.new(10)

binding1

=

obj1.getBinding

obj2

=

BindingTest.new(“Binding

Test”)

binding2

=

obj2.getBinding

puts

eval(“@value”,

binding1)

#=>

10

puts

eval(“@value”,

binding2)

#=>

Binding

Test

puts

eval(“@value”)

#=>

nil

可以看到上述代码中,@value在binding1所指明的上下文环境中值为10,在binding2所指明的上下文环境中值为Binding

Test。当eval方法不提供binding参数时,在当前上下文环境中@value并未定义,值为nil。