Recently i tried to install vagrant on my ubuntu box and met some problem. I know ruby very little, perhaps someone can explain there problem. After installing vagrant by gem i can not run it directly, this is because vagrant is in /var/lib/gems/1.8/gems/vagrant-0.8.2/bin/vagrant which is not in my PATH. Ok link this file to /usr/bin [...]
Archives for Ruby
Rails on Apache
rails在apache里是以cgi或是fastcgi的形式运行的。配置方法如下 配置apache使其可以在cgi-bin目录外可以执行cgi或是fcgi程序。把 AddHandler cgi-script .cgi 加入到apache的配置文件里面。 把rails产生的public目录添加到服务器里。 Alias /ror/ “/your/ror/dir/public/” Options Indexes MultiViews AllowOverride None Order allow,deny Allow from all 要注意的是: 发布的是rails产生的public目录
Ruby data dumper
在Perl和PHP里都有显示复杂数据的函数(Perl 里可以用Data::Dumper, PHP可以用var_dumper(), 或是print_r。在Ruby里可以使用.inspect。为了显示出来必须还要使用 puts。 a = [1, 2, 3] puts a.inspect 还可以使用pp模块(pretty pring) require(“pp”) a = [1, 2, 3, 4] pp a # [1, 2, 3, 4]
Ruby 5 (class)
ruby里面继承用 < class SunC < ParentC end 可以在任意时候给任意类添加方法,用法简单如: class SunC def someNew end end 类方法的定义: 类名字.方法名 class AClass def AClass.classMethode end end
Ruby 4 (Regexp)
Ruby的regexp,和perl 一样。使用的时候有不同。 例如: 替换一个字符窜 , perl s/someString/replaceString/, ruby 是:astring.sub(/apattern/, ‘areplacestring’)
Ruby 3 (数组,散列)
定义数组: a = ['ab', 1, 'ting'] 或是 a = %w/some word sperate with blank/ 定义散列: h = {‘name1′=>’ting’, ‘name2′=>’tiw’} 使用: a[0] 或是 h['name1']
Ruby 2 (块)
由 {} 或是 do … end 包围起来的一段代码叫做 block。在ruby里,可以把一个block附加到一个函数上。在被附加的函数里,可以使用yield调用附加的block。例如: def print_sum(n, m) puts “the summer of n and m is: #{yield(n, m)}” end print_sum(2, 1){|f, s| f+s} 会显示: the summer of n and m is: 3
Ruby 1 (变量)
局部变量 name 全局变量 $name 对象变量 @name 类变量 @@name