Problen4-Project Euler
11月 3rd, 2009
未分类
No Comments
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
(999*999).downto(100*100) do |i| break puts i if i.to_s == i.to_s.reverse end
Problem3 – Project Euler
11月 3rd, 2009
未分类
2 Comments
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
require 'mathn'
class Integer
def prime_factors
x = self
prime_gen = Prime.new
factors = prime_gen.each do |prime|
(puts prime || [] << prime) if x % prime == 0
end
puts "the prime factors of #{self} #{factors}"
end
end
13195.prime_factors
Riddle by Einstein
11月 2nd, 2009
未分类
No Comments
Let us assume that there are five houses of different colors next to each other on the same road. In each house lives a man of a different nationality. Every man has his favorite drink, his favorite brand of cigarettes, and keeps pets of a particular kind.
1. The Englishman lives in the red house.
2. The Swede keeps dogs.
3. The Dane drinks tea.
4. The green house is just to the left of the white one.
5. The owner of the green house drinks coffee.
6. The Pall Mall smoker keeps birds.
7. The owner of the yellow house smokes Dunhills.
8. The man in the center house drinks milk.
9. The Norwegian lives in the first house.
10. The Blend smoker has a neighbor who keeps cats.
11. The man who smokes Blue Masters drinks bier.
12. The man who keeps horses lives next to the Dunhill smoker.
13. The German smokes Prince.
14. The Norwegian lives next to the blue house.
15. The Blend smoker has a neighbor who drinks water.
The question to be answered is: Who keeps fish?
Einstein wrote this riddle early during the 19th century. He said 98% of the world could not solve it. Its not hard, you just need to pay attention and be patient.
class Person attr_accessor :nationality, :residing, :drinking, :smoking, :pet, :number def initialize(number, residing = nil) @number = number @residing = residing end end class Residing attr_accessor :number, :host, :color def initialize(number, color = nil, host = nil) @number = number @host = host @color = color end end residing_1 = Residing.new(1, "yellow") # making use of condition 9, 7 person_1 = Person.new(1, residing_1) person_1.nationality = "Norway" #making use of condition 9 person_1.smoking = "DUNHILL" #making use of condition 7 person_1.pet = "cat" #"cat" residing_2 = Residing.new(2, "blue") # making use of condition 14 person_2 = Person.new(2, residing_2) person_2.pet = "horse" # condition 11 person_2.nationality = "Denmark" person_2.drinking = "tea" person_2.smoking = "blend cigarette" #"Denmark" "tea" "blend" residing_3 = Residing.new(3, nil) #making use of condition 8 person_3 = Person.new(3, residing_3) person_3.nationality = "English" person_3.drinking = "milk" #making use of condition 8 person_3.residing.color = "red" #making use of conditon 1 person_3.smoking = "PALL MALL" person_3.pet = "bird" #"PALL MALL" "bird" residing_4 = Residing.new(4, "green")# making use person_4 = Person.new(4, residing_4) person_4.drinking = "coffee" # making use of condition 5 person_4.nationality = "German" person_4.smoking = "PRINCE" #"Germ" "PRINCE" residing_5 = Residing.new(5, "white")# making use of condition 9,14,4 person_5 = Person.new(5, residing_5) person_5.nationality = "Sweden" person_5.pet = "dog" person_5.smoking = "BLUE MASTER" person_5.drinking = "beer" #"Sweden" "dog" #"BLUE MASTER" "beer" #after the initialization of the status,input as the followings arr = [person_1, person_2, person_3, person_4, person_5] arr.each do |i| i.pet ||= "fish" puts "Nationality: #{i.nationality}; house-color: #{i.residing.color}, drink #{i.drinking}, smoke #{i.smoking}, raise the pet #{i.pet} " end
“Sinatra网络应用程序开发”将于12月份开课
10月 22nd, 2009
培训信息
No Comments
我们的“Sinatra网络应用程序开发”培训马上就要开课啦!
我们将为此课程安排一个试听的Session,时间是本次培训的第一次课。因为试听Session的名额有限(暂定20-40人),请事先到我们的网站进行报名,我们将优先安排在网上进行报名的学员参加试听课程。
我们将在第一次试听课程结束的时候进行培训的现场报名,如果你没来得及在线报名或错过了我们是试听课程,请电话联系我们:0510-83594020。
请所有有兴趣参加我们课程培训的关注我们的网站,并及时报名。对我们的课程有任何意见和建议请在此留言。谢谢!
:)
闲看RUBY内嵌对象(二): Fixnumx,true,false,nil和undef
10月 13th, 2009
技术文档
No Comments
上次和大家一起讨论了Ruby的立即对象(immediate objects)之一:符号对象(Symbol),今天一并把其余的几个immediate objects给收拾了。
立即对象都是Ruby世界中轻量却不容小觑的人物,符号对象在Rails对码中随处可见,这个先按下不表。先贴出Fixnum,true,false,nil的C层次构造代码:
false true nil undef(ruby.h)
#define Qfalse ((VALUE)0) /*false*/ #define Qtrue ((VALUE)2) /*true*/ #define Qnil ((VALUE)4) /*nil*/ #define Qundef ((VALUE)6) /* undefined value for placeholder */
Fixnum(ruby.h)
#define FIXNUM_MAX (LONG_MAX>>1) #define FIXNUM_MIN RSHIFT((long)LONG_MIN,1) #define FIXNUM_FLAG 0x01 #define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG)) #define LONG2FIX(i) INT2FIX(i) #define FIX2INT(x) rb_fix2int((VALUE)x) #define FIXNUM_P(f) (((long)(f))&FIXNUM_FLAG)
再看看ruby是怎样判断某一对像是否为immediate object的,大家还记得上次说到的VALUE这个类型定义吗?当给定一个 VALUE 对象时,使用 TYPE 宏判断该对象的种类,例如 Symbol 对象类型为T_SYMBOL。
Read more »
闲看RUBY内嵌对象(-): Symbol
09月 30th, 2009
未分类
No Comments
我们在常听说符号类是ruby采用的一种轻量级的解决方案,可究竟如何轻量呢,为何在Rails中Symbol对象的身影随处可见呢; [...].map(&:name)、%w[mocha heckle].each(&method(:gem))背后又发生了哪些事情呢 ?
首先让我们来感受一下Ruby内部的符号表:
>> original_symbols_size = Symbol.all_symbols.size
=> 1776
>> class Test
>> attr_accessor :test
>> end
=> nil
>> updated_symbols_size = Symbol.all_symbols.size
=> 1779
>>
咦,多了俩
咱们来把那俩揪出来瞅瞅
>> class Test
>> attr_accessor :test
>> end
=> nil
>> Symbol.all_symbols – original_symbols
=> [:Test, :@test]
>>
可见Ruby把symbol存放在运行时维护的一个符号表里了,而这个符号表实际上是一个atom数据结构,其中存储着当前所有的程序级的identifier,确保不出现内容相同的多个对象。如果你再用一个已在符号表中“备案”的标识符去作类名,结果就是重新打开那个类,添加其定义体。
接着再让我们看看Symbol缘何能轻量,下面是它的C层次代码:
Symbol (ruby.h)
typedef unsigned long ID;
#define SYMBOL_FLAG 0×0e
#define SYMBOL_P(x) (((VALUE)(x)&0xff)==SYMBOL_FLAG)
#define ID2SYM(x) ((VALUE)(((long)(x))<<8|SYMBOL_FLAG))
#define SYM2ID(x) RSHIFT((unsigned long)x,8)
其中 VALUE 的typedef 是这样的
typedef unsigned long VALUE
到此我们即可看出Symbol对象就是存储为一个直接值嘛,果然轻量!
今天就切磋到这,最后再来一句意味深长,含义久远的::
Symbol对象代表代码解析树里的每个token。
Ruby学习(中国)官方博客开张
09月 29th, 2009
网站公告
No Comments
Ruby学习(中国)网站目前正在建设中,本博客暂时作为官方站点开放。等Ruby学习(中国)的主站建设完成,这里将是其中的一个子站。

Ruby学习(中国)是RubyLearning的官方中文站,致力Ruby程序设计语言和Ruby on Rails,以及其他Ruby网络应用程序开发框架在中国的教育和普及。
本博客将用作Ruby,Rails,以及其他Sinatra,Merb等,Ruby相关应用和开发的资讯、教程。当然,这里也是获得Ruby学习(中国)提供的培训信息的地方。
RubyLearning.com是有Satish Talim先生所创办;Ruby学习(中国)由无锡天狗软件开发公司管理和维护。