- 浏览: 238176 次
文章分类
最新评论
-
bluky999:
中间的兼职例子很逗 哈哈哈
tornado: web.py 之 Application -
flingfox63:
学习了,详细,赞个
Ruby变量作用域的类目录结构 -
zhou6711411:
不知是版本问题还是怎么的
class A
...
Ruby变量作用域的类目录结构 -
t284299773:
你在方法中定义方法就相当于在方法中调用lambda!
Ruby变量作用域的类目录结构(补二) -
lnj888:
很是有用 不错Powerpoint converter
一个简单的link_to,ROR到底在背后做了些什么?(未完)
1。使用h=hash.new(0)可以把hash中的每一个元素都初始化为0
2。类的实例变量能够在类内部任意位置定义(@xxx),而且可以被类内方法访问。A class variable is shared among all objects of a class, and it is also accessible to Class variables are private to a class and its instances. If you want to make them accessible to the outside world, you’ll need to write an accessor method the class methods that we’ll describe later.
3。$: ruby的环境变量查询路径
4。class << 表示向类中添加方法,例如:
- class A
- class << self
- def its_nothing_happned
- end
- end
- end
调用A.methods可以看到A中有了名为its_nothing_happned这个类方法
5。ruby中的private方法和Java、C#中的方法不同!见代码:
#ruby代码
- class A
- private
- attr_access :nth
- #注意,这个方法是错的!
- def compare(class_a_obj)
- if @nth == class_a_obj.nth #不可以访问另外一个A的私有字段!!
- true
- else
- false
- end
- end
- end
但是java和C#都可以访问:
- class Test
- {
- private string name;
- public Test(string name)
- {
- this.name = name;
- }
- public override bool Equals(object obj)
- {
- if (this.name == ((Test)obj).name)
- {
- return true;
- }
- return false;
- }
- }
- class Test
- {
- private String name;
- public Test(String name)
- {
- this.name = name;
- }
- public boolean equal(Test t)
- {
- if(this.name == t.name)
- {
- return true;
- }
- return false;
- }
- }
6。类可以在类内部访问此类所有实例的protected类型变量:
- class Account
- attr_reader :balance # accessor method 'balance'
- protected :balance # and make it protected
- def greater_balance_than(other)
- return @balance > other.balance
- end
- end
7。A variable is simply a reference to an object,variable is not a object.
8。ruby虽然每个元素都是object,包括int,float等,但是这些基本类型处理起来似乎和C#有几分相似,例如:
- s1 = "Jim"
- s2 = s1
- s2[0] = "T"
- puts s1 #Tim
- puts s2 #Tim
- a = 1
- b = a
- b = 2
- puts a #1
9。数组中可以用array[-1]来代替数组的最后一个元素,用array[1,-1]来代表数组中第一个到最后一个元素。
10。注意这段程序,关于block中使用block外定义的变量问题:
- a = [1, 2]
- b = 'cat'
- a.each {|b| c = b * a[1] }
- a #[1, 2]
- b 2
- defined?(c) nil
再看这段:
- arr = []
- def nth
- yield
- end
- nth { arr = %w{1 2 3 4 5 6} }
- p arr #["1", "2", "3", "4", "5", "6"]
其中的问题在于block里面使用了于外面定义同名的变量,而且使用外部变量在block前,不过在ruby2.0中会改变这个block中变量继承外部变量的方式。
11。用?xxx的方式获得xxx对应的int值,这里xxx是一个ASCII字符,例如?a, ?C-M-a(Ctrl+Alt+a)
12。在判断语句中使用range
- strs = []
- strs << "hello"
- strs << "start"
- strs << "haha"
- strs << "end"
- strs.each do |str|
- puts str if str=~/start/..str=~/end/
- end
输出为:
start
haha
end
13。If you define a method inside another method, 1.8 the inner method gets defined when the outer method executes.
14。ruby赋值的方式可谓多种多样,如下:
- arr = [1,2,3,[4,5,6]]
- a,b,c,d = arr
- p a #1
- p b #2
- p c #3
- p d #[4,5,6]
给数组赋值:
- arr = []
- arr = 1,2,3,[4,5,6]
- p arr #[1, 2, 3, [4, 5, 6]]
15。ruby1.8中virtual viarable的赋值方法返回值发生了变化:
- class A
- def name=(value)
- @name = value
- return 99
- end
- def name
- return @name
- end
- end
- a = A.new
- p a.name = 20 #20
可以看到,使用赋值方法返回的值一定是传入参数的值,而跟在赋值方法中指定的返回值无关。但是在ruby1.8之前的版本,赋值之后返回的值是赋值方法最后一条执行语句的返回值。
16。注意并行赋值的一点儿细节(注意b、c和y的值):
- x = 0
- a, b, c = x, (x += 1), (x += 1)
- y = 0
- y,y,y = x,(x+=1),x+=1
- p a #0
- p b #1
- p c #2
- p x #4
- p y #4
17。参数前缀的意义,&表示将这个参数看作一个pro,*表示将此参数包装成一个数组:
- p1 = 2
- p2 = {:name=>"jack"}
- p3 = [3,4]
- a,b = p1,p2
- p a
- p b
- a,*b = p1,p3
- p a
- p b
- a,b=p1,*p2
- p a
- p b
结果:
2
{:name=>"jack"}
2
[[3, 4]]
2
[:name, "jack"]
18。并行赋值的tips:Nested Assignments Parallel assignments have one more feature worth mentioning. The left side of an assignment may contain a parenthesized list of terms. Ruby treats these terms as if they Prepared exclusively for Yeganefar CONDITIONAL EXECUTION 87 were a nested assignment statement. It extracts the corresponding rvalue, assigning it to the parenthesized terms, before continuing with the higher-level assignment.
b, (c, d), e = 1,2,3,4 ! b == 1, c == 2, d == nil, e == 3
- b, (c, d), e = [1,2,3,4] ! b == 1, c == 2, d == nil, e == 3
- b, (c, d), e = 1,[2,3],4 ! b == 1, c == 2, d == 3, e == 4
- b, (c, d), e = 1,[2,3,4],5 ! b == 1, c == 2, d == 3, e == 5
- b, (c,*d), e = 1,[2,3,4],5 ! b == 1, c == 2, d == [3, 4], e == 5
19。在正则表达式中,$~用来储存MatchData,其中有关于MatchData的详细信息。同时$1~$9也用来储存多匹配时的多个MatchData。
20。正则表达式:[]的开始加上^表示negative,例如"bcd"=~/[^aeiou]/ =>b
21。
r{m,n} matches at least “m” and at most “n” occurrences of r.
r{m,} matches at least “m” occurrences of r.
r{m} matches exactly “m” occurrences of r.
22。在重复符号后面加 ? 取消贪婪性
show_regexp(a, /\s.*?\s/) => The<< moon >>is made of cheese
23。
Parentheses also collect the results of pattern matching. Ruby counts opening parenthe-
ses, and for each stores the result of the partial match between it and the corresponding
closing parenthesis. You can use this partial match both within the rest of the pattern
and in your Ruby program. Within the pattern, the sequence \1 refers to the match of
the first group, \2 the second group, and so on. Outside the pattern, the special variables
$1, $2, and so on, serve the same purpose.
"12:50am" =~ /(\d\d):(\d\d)(..)/ => 0
"Hour is #$1, minute #$2" => "Hour is 12, minute 50"
"12:50am" =~ /((\d\d):(\d\d))(..)/ => 0
"Time is #$1" => "Time is 12:50"
"Hour is #$2, minute #$3" => "Hour is 12, minute 50"
"AM/PM is #$4" =>"AM/PM is am"
24。在我们定义一个方法的时候,如果在最后一个形参前面加上*,则表示传入的实参多余形参个数的时候,多余的实参会被包装成一个数组,例如:
def meth(*a)
if a.is_a?Array
puts a.join(' ')
else
puts a
end
end
meth(1) => 1
meth(1,2,3,4,5) => 1 2 3 4 5
与上面对应的,在我们传入参数的时候,如果在属性类型的实参前面加上*,则表示将会把数组中的元素释放出来,数组中的每一个元素都会被当作一个实参对待,例如:
def meth(a,b,c)
puts "#{a} #{b} #{c}"
end
meth(*[1,2,3]) => 1 2 3
25。在对方法传参的时候,假如传入的参数列表中最后几项都是hash的key=>value形式,那么ruby会自动把这些key=>value包装到一个hash表中作为一个实参,例如:
def meth(a,b)
p a
p b
end
meth("L.A",:name=>"jack",:age=>46,:sex=>'male')
=>"L.A"
=>{:name=>"jack",:age=>46,:sex=>"male"}
=>nil
需要注意的是这些key value对必须出现在实参列表的最后,以上面的程序为例子,假如我们改变输入顺序:
meth(:name=>"jack",:age=>46,:sex=>'male',"L.A")
ruby的编译器会出错
发表评论
-
Ruby变量作用域的类目录结构(补二)
2007-09-25 13:36 2352ruby中,类中可以定义类 ... -
动态改变方法体,领略ruby强大的动态编程能力
2007-09-16 13:15 1339ruby 代码 class A ... -
Ruby变量作用域的类目录结构(补)
2007-09-11 16:53 1252ruby 代码 class A ... -
Ruby变量作用域的类目录结构
2007-09-11 15:59 4054在ruby代码中,变量拥有 ... -
ruby类继承中的方法继承
2007-09-11 12:27 2037c# 代码 namespace ConsoleA ... -
实例方法和类方法(补)
2007-08-31 18:19 1150看到一个人这样用module,让我迷糊了半天 ruby 代码 ... -
实例方法和类方法(三)
2007-08-31 12:56 1247这一次,让我们搞一些破坏性实验,来验证上两次的内容 第一个破坏 ... -
实例方法和类方法(二)
2007-08-31 11:35 1313ruby中用mixin技术来把类和module揉起来,例如 r ... -
实例方法和类方法(一)
2007-08-31 11:01 3666Ruby中,有两个很重要的 ... -
Re: 如何用module_eval向class中添加instance variable
2007-08-29 09:34 1265ruby 代码 class A ... -
关于单件类
2007-08-27 19:08 1338主要资料来自于http://ola-bini.blogspot ... -
关于extend和include
2007-08-27 12:13 1159ruby 代码 module Action ... -
看到一片老文章,关于cattr_accessor和class_inheritable_accessor
2007-08-15 16:18 1544So, cattr_accessor doesn’t work ... -
动态生成Proc,看了一篇名为The Methodphitamine 的文章
2007-08-13 14:17 1132这篇文章中,采用了对to_proc hack的方式实现了一种更 ... -
偶然搜到DHH给Matz的一封信,关于undef_method和remove_method
2007-08-13 11:11 1220Hi, In message "[ruby-tal ... -
并非只能使用%w(a b c d e f)
2007-08-13 10:49 1111并非只能使用%w(a b c d e f),可以把括号换成任何 ...
相关推荐
Programming Ruby 2nd Edition
学习ruby的绝佳入门书籍,Programming Ruby 2nd.pdf
Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]
Programming Ruby 2nd Edition
内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm
Programming Ruby中文版第二版[高清扫描版][带书签]。 Programming.Ruby-2nd[高清文字版][带
上传一系列RUBY的书,基本上网上能见到的全了
Programming Ruby, Ruby程序设计
NULL 博文链接:https://huihuilou.iteye.com/blog/1997808
Pragmatic Programmers - Programming Ruby(2nd Edition Syngress).rar
Programming Ruby 2.0 1.9 pdf
Programming ruby.pdf 英文的,不过觉得读起来不吃力,习惯就好了,程序员如果不习惯读英文,会错过非常多的好书
Programming Ruby 1.9 (3rd edition)和源码
Programming Ruby English version and the source Code
Programming Ruby 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...
PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版
学习Ruby最好的书,俗称镐头书,受用无限。
Boost.Asio C++ Network Programming(2nd).pdf Boost.Asio C++ Network Programming(2nd).pdf Boost.Asio C++ Network Programming(2nd).pdf