ruby
aotian16
这个作者很懒,什么都没留下…
展开
-
ruby安装mysql2遇到的问题及解决方法
安装mysql2的时候遇到问题E:/application/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.2.18-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require': Incorrect MySQL client library version! This gem was compiled for 6.0.原创 2012-02-08 13:31:13 · 4248 阅读 · 0 评论 -
ruby入门_构造方法
initialize, 构造方法class Person def initialize name,age @name = name @age = age end def getName return @name end def getAge return @age endendtom = Person.new "Tom"原创 2012-02-24 15:46:14 · 1644 阅读 · 0 评论 -
ruby入门_if
没啥好说的num = 90if num < 60 puts "fight"elsif num < 90 puts "good"else puts "nice"end输出nice要注意的是elsif原创 2012-02-23 16:00:23 · 839 阅读 · 0 评论 -
ruby入门_while
没啥好说的循环i = 5while i > 0 puts i i=i-1end输出54321原创 2012-02-23 16:22:35 · 1117 阅读 · 0 评论 -
ruby入门_break
break, 结束循环i = 5while i > 0 puts i i=i-1 if i == 2 break endend输出543原创 2012-02-24 11:06:14 · 1420 阅读 · 0 评论 -
ruby入门_next
next, 相当于java的continue结束本次循环i = 5while i > 0 i=i-1 if i == 2 next end puts iend输出4310原创 2012-02-24 11:12:17 · 1420 阅读 · 0 评论 -
ruby入门_redo
redo, 类似next但是他继续下次循环的时候, 不检查条件i = 5while i > 0 i=i-1 if i == 0 redo end puts iend输出4321-1原创 2012-02-24 11:21:50 · 1284 阅读 · 0 评论 -
ruby入门_string
string,字符串需要注意双引号 "" 和单引号'' 的区别。双引号会对转义字符和特殊字符进行转换单引号只有少量转换var1 = "test"var2 = 'test'var3 = "#{var1}"var4 = '#{var1}'puts var1 == var2puts var1.object_id == var2.object_idputs "v原创 2012-02-24 13:40:13 · 659 阅读 · 0 评论 -
ruby入门_case
看例子就可以了num = 90case numwhen 0..59 puts "fight"when 60..89 puts "good"when 90..99 puts "nice"when 100 puts "best"elseputs "something wrong!!!"end原创 2012-02-23 16:16:34 · 514 阅读 · 0 评论 -
ruby入门_until
until直到...i = 5# == while notuntil i == 0 puts i i=i-1end输出54321原创 2012-02-23 16:28:09 · 1270 阅读 · 0 评论 -
ruby入门_each
迭代器对比for in看看people1 = ["Tom", "Marry", "Jack", "Kay"]people2 = {"Tom"=>23, "Marry"=>22, "Jack"=>25, "Kay"=>20}# ------------------------people1.each do |person| puts personend# ---------原创 2012-02-23 16:56:04 · 7177 阅读 · 0 评论 -
ruby入门_类
class , 类# classclass Person # method def getName return "Tom" endendtom = Person.newputs tom.getName输出Tom原创 2012-02-24 15:18:07 · 497 阅读 · 0 评论 -
ruby入门_类方法
# classclass Person # class method def Person.Moveable return true endendputs Person.Moveable输出true原创 2012-02-24 15:27:27 · 449 阅读 · 0 评论 -
ruby入门_属性
class Person def initialize name,age,sex @name = name @age = age @sex = sex end # read attr_reader :name # write attr_writer :age # read and write attr_accessor :sexendt原创 2012-02-24 16:00:23 · 775 阅读 · 0 评论 -
ruby入门_访问作用域
默认,共有,私有,保护class Person def foo puts 0 end public def pubFoo puts 1 end private def priFoo puts 2 end protected def proFoo puts 3 endendtom = Person.ne原创 2012-02-24 16:32:06 · 938 阅读 · 0 评论 -
ruby入门_没有重载方法
ruby没有重载方法class Person def says puts "there is no overload in ruby!" end def says(word) puts word endendperson = Person.new# here is wrong# person.saysperson.says("hey!")原创 2012-02-29 13:22:59 · 1528 阅读 · 0 评论 -
ruby入门_继承
ruby是单继承的。可以重写基类的方法class Father def says puts "I am father." end def fatherInfo puts "father info." endendclass Son < Father def says puts "I am son." end de原创 2012-02-29 13:09:03 · 3364 阅读 · 0 评论 -
ruby入门_类变量
使用@@class Person @@name = "Tom" def Person.getName return @@name endendputs Person.getName原创 2012-02-24 15:41:56 · 964 阅读 · 0 评论 -
ruby入门_实例变量
使用@ss Person @name def getName return @name end def setName name @name = name endendtom = Person.newtom.setName "Tom"puts tom.getName原创 2012-02-24 15:36:47 · 734 阅读 · 0 评论 -
ruby入门_方法
要注意参数,可变长度参数,和参数默认值还有对象的特殊方法class Person def say1(word1,word2) puts word1+word2 end # variable var def say2(*word) puts word end # default var def say3(word = "someth原创 2012-02-24 15:24:44 · 475 阅读 · 0 评论 -
ruby入门_变量
如代码所示变量无需定义, 很方便但是变量多了不知道会不会搞不清楚!r = 2pi = 3.14s = pi*r*rputs "s = #{s}"#{s}是占位符原创 2012-02-10 13:07:31 · 618 阅读 · 0 评论 -
ruby入门_常量
ruby要求常量首字母要大写但是为了可读性,建议全部大写而且, 说是常量, 却是可变的, 但是会有警告PI = 3.14puts "PI = #{PI}"# ----------------PI = 314puts "PI = #{PI}"控制台会输出PI = 3.14E:/application/Aptana/Aptana Studio 3/workspa原创 2012-02-10 13:17:33 · 728 阅读 · 0 评论 -
ruby入门_全局变量
全局变量,顾名思义全局可使用的变量可能引起高耦合,建议少使用$var = "project var"puts "$var = #{$var}"还有ruby自带一些全局变量ruby自带全局变量$! 最近一次的错误信息$@ 错误产生的位置$_ gets最近读的字符原创 2012-02-10 13:31:48 · 2138 阅读 · 0 评论 -
ruby入门_并行赋值
方便的赋值交换变量倒是快捷a,b=1,3puts a,b# ------a,b=b,aputs a,b输出1331原创 2012-02-10 13:50:06 · 830 阅读 · 0 评论 -
ruby入门_运算符
讲几个1,乘方,即幂,**var = 2puts var**1puts var**2puts var**3puts var**4输出248162,对象比较var = 2puts var1puts var2puts var3输出10-1即,大于,等于,小于时分别返回1,0,-1原创 2012-02-10 14:36:32 · 854 阅读 · 0 评论 -
ruby入门_HelloWorld
可以通过与java的对比看看ruby的简洁java版public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world."); }}ruby版puts "Hello world."确实简洁了很多啊!原创 2012-02-10 12:48:36 · 3334 阅读 · 0 评论 -
ruby入门_并行赋值_数组
看几个程序1,普通变量赋值arr=[1,2,3,4,5]a,b=arrputs "a=#{a},b=#{b}"输出a=1,b=22,数组赋值,使用了*arr=[1,2,3,4,5]a,*b=arrputs "a=#{a},b=#{b}"输出a=1,b=[2, 3, 4, 5]可以看到b是一个赋值完a后的剩余的数组原创 2012-02-10 14:19:27 · 3190 阅读 · 0 评论 -
ruby入门_范围
即Range比如,1..3 ,1...3两个点包括末个数值3三个点不包括3r1 = 1..5r2 = 1...5r1.each do |var| print var.to_s+","endputs "------"r2.each do |var| print var.to_s+","endputs "------"输出1,2,3,4,5原创 2012-02-10 14:52:57 · 697 阅读 · 0 评论 -
ruby入门_哈希表
就是一个键值对对象# defineperson={ "name" => "Tom", "age" => "20", "sex" => "male", 1 => 1, 2 => "abc"}# get valueputs "name = #{person["name"]}"puts "age = #{person["age"]}"puts "sex = #{pe原创 2012-02-10 17:33:31 · 922 阅读 · 0 评论 -
ruby入门_符号
symbol,即符号是字符串,方法,类等对象的名字var1 = "hello"var2 = :var1puts var1 # helloputs var2 # var1# -------------------var1 = "changed"var3 = :var1puts var1 # changedputs var2 == var3 # trueput原创 2012-02-23 15:49:58 · 696 阅读 · 0 评论 -
ruby入门_unless
unless,即,除非,如果不相当于if notvar = trueif var puts "if"endunless !var puts "unless == !if"end原创 2012-02-23 16:10:52 · 1402 阅读 · 0 评论 -
ruby入门_for_in
针对数组和哈希表看例子就可以了people1 = ["Tom", "Marry", "Jack", "Kay"]people2 = {"Tom"=>23, "Marry"=>22, "Jack"=>25, "Kay"=>20}# ------------------------for person in people1 puts personend# ------------原创 2012-02-23 16:47:27 · 1448 阅读 · 0 评论 -
ruby入门_loop
loop, 无条件循环有俩种写法, 效果一样i = 5loop{ if i == 0 break else puts i i = i - 1 end}# ---------------i = 5loop do if i == 0 break else puts i i = i - 1 endend输出原创 2012-02-24 11:03:18 · 3091 阅读 · 0 评论 -
ruby入门_retry
retry, 重新迭代arr = [1,2,3,4]flg = truearr.each do |num| puts num if num == 3 && flg flg = false retry endend输出1231234原创 2012-02-24 11:37:10 · 1565 阅读 · 0 评论 -
ruby入门_array
array, 数组两种初始化arr1 = [1,2,3,4]arr2 = Array.newarr2[0] = 4arr2[1] = 5arr2[2] = 6arr2[3] = 7arr2[4] = 8puts arr1.lengthputs arr1.sizeputs arr1[1]puts arr1[2]puts arr1[-2]print arr1输出原创 2012-02-24 14:32:31 · 628 阅读 · 0 评论 -
ruby入门_特殊类
让对象拥有特殊变量或者方法class Person def getName return "Tom" endendtom1 = Person.newtom2 = Person.new# special classclass << tom1 def getAge return 20 endendputs tom1.getAge# error原创 2012-02-24 15:20:52 · 559 阅读 · 0 评论 -
ruby入门_模块
模块(module)和类同一级和类类似,但是不能实例化可以被类包含self可以指定模块方法module DemoModel def foo1 # common method puts "foo1. common method" end def self.foo2 # module method puts "foo2. module method"原创 2012-02-29 14:39:36 · 936 阅读 · 0 评论