易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : Ruby

Ruby快速入门(二):变量

上一篇文章
说了Ruby的安装和运行,也简单的说了下类和对象。这里主要谈谈变量的问题。
先说常量
。如果变量名以大写字母开头,就被视为常量,但通常是所有字母都大写。但和其他语言不同,在Ruby中,你仍然可以改变常量的值,当然解释器会抛出一个警告:
#! /usr/bin/ruby
CONSTANT = 1
print "#{CONSTANT}\n"
CONSTANT += 1
print "#{CONSTANT}\n"

这段代码定义了一个名为CONSTANT的常量,并初始化为1。然后对其的改动却是被允许的,但解释器会抛出一个如下所示的警告:
./2.rb:4: warning: already initialized constant CONSTANT
现在开始看变量
。在Ruby中,变量都是“无类型”的,不用指定其具体类型,解释器会根据其值自行判断。但Ruby也提供了一些方法来判断变量的当前类型:
print "#{1.class}\n"
print "#{1.kind_of? Integer}\n"

第一行代码打印变量1的类型(还记得在Ruby中,一切都是对象吧?!),第二行则是打印1是否是Integer的结果。
同时,Ruby还提供了一些函数进行变量类型的转换,如整数可调用to_f函数转化为浮点数、调用to_s函数转化为字符串等等。
现在看看变量的作用域
。 ......

Ruby 101:方法对象


Ruby 101:方法对象
Written by Allen Lee
从方法调用说起
      在上一篇文章里,我们看到调用对象的方法实质上是向对象发送消息,下面,我们再来看一个有趣的应用。在Ruby里,字典可以通过 {key => value} 来创建,如果你使用的版本是1.9或以上,当key的类型是Symbol时,创建字典的语法可以进一步简化为 {key: value} (注意,冒号要紧贴在key后面),这使得我们可以创建这样的对象:

代码 1
有没有觉得这个写法很面熟?有些同学可能已经看出来了,这个写法很像JSON,事实上,这个代码正是仿照Wikipedia上的JSON示例代码写出来的,然而,由于它本身是一个字典对象,在访问里面的内容时需要使用字典的语法:

代码 2
现在,请思考一下,有没有办法使它接受这样的做法呢:

代码 3
有些同学可能已经反应过来了——打开Hash类,重写method_missing方法:

代码 4
当我们调用first_name和phone_numbers等不存在的方法时,就会触发method_missing方法,它首先检查字典是否包含这个方法名,若是,返回对应的值,否则,转交super处理。下面,我们执行一下代码:

图 1
很好,基本上达到我们的预期了 ......

win环境下本地安装ruby on rails

过程如下:
1、ruby下载一键安装:
http://rubyforge.org/frs/download.php/29263/ruby186-26.exe
ruby -v 显示版本,安装成功
2、下载rubygems安装:
http://rubyforge.org/frs/download.php/60719/rubygems-1.3.5.zip
解压,ruby setup.rb
gem -v  显示版本,安装成功
3、下载rails的 gem 安装(gem install -l  本地安装):
http://rubyforge.org/frs/download.php/63167/rails-2.3.4.gem 
 
gem install -l rails-2.3.4.gem 
 
会提示依赖:
rails requires rake (>= 0.8.3, runtime
 
下载
rake:
http://rubyforge.org/frs/download.php/56871/rake-0.8.7.gem
 
继续
gem install -l rails-2.3.4.gem
提示依赖:
rails requires activesupport (= 2.3.4
下载
activesupport :
http://rubyforge.org/frs/download.php/63162/activesupport-2.3.4.gem
 
如此,依次下载所有依赖:
activerecord:
http://rubyforge.org/frs/download.php/63163/activerecord-2.3.4.gem
actionpack:
http://rubyforge.org/frs/download.php/63164/actionpack-2.3.4.gem
rack:
http://rubyforge.or ......

最详细的Ruby on Rails安装步骤


本文介绍了Ruby on Rails的详细安装步骤。文中使用的Ruby on Rails版本是1.8.6-26。安装Ruby之后安装Rails,然后就可以创建Web应用,并在本地测试了。
1、安装ruby
不用说 是下载安装包:http://rubyforge.org/frs/?group_id=167,注意版本---害人不浅,后面会说到,我下的是1.8.6-26
装完后,可以用ruby -v 测试是否安装成功,如图,要是出现了版本 就说明安装成功了
 
2、ruby安装完成后,安装rails
在命令行下运行 gem install rails,会有四五个提示 一路Y,安装完后,依旧可以用rails -v, 来检测rails 是否安装成功
 
3、创建Web应用
还是命令行下输入 rails testweb,比如当前的路径是 C:\Documents and Settings\Administrator>rails testweb,便会在C:\Documents and Settings\Administrator下创建一个testweb文件夹。
 
 
PS:这里是可以加路径的 比如 rails C://test,具体可以看官方API:http://api.rubyonrails.org/
4、在当前目录(testweb)下,运行 ruby script\server 启动服务,关于webrick服务器更多的信息可以访问 http://www.webrick.org/
 
5、浏览器中输入http://localhost:3000/,如果看到以下页面,说 ......

c++中调用ruby代码

#include < ruby.h > //
static int id_sum;
int Values[] = {5, 10 ,15,-1,20,0};
static VALUE wrap_sum(VALUE args)
{
 VALUE * values = (VALUE *) args;
 VALUE summer = values[0]; 
 VALUE max = values[1];
 return rb_funcall(summer,id_sum,1,max);
}
static VALUE protected_sum(VALUE summer, VALUE max)
{
 int error;  VALUE args[2];
 VALUE result;
 args[0] = summer;
 args[1] = max;
 result = rb_protect(wrap_sum,(VALUE)args, &error);
 return error ? Qnil:result;
}
int main()
{
 int value;
 int *next = Values;
 ruby_init();
 ruby_init_loadpath();
 ruby_script("embedded");
 rb_require("sum.rb");
 VALUE summer = rb_class_new_instance(0,0,rb_const_get(rb_cObject,rb_intern("Summer")));
 id_sum = rb_intern("sum");
 while (value = *next++) 
 {
  VALUE result = protected_sum(summer,INT2NUM(value)); 
  if (NIL_P(re ......

c++中调用ruby代码

#include < ruby.h > //
static int id_sum;
int Values[] = {5, 10 ,15,-1,20,0};
static VALUE wrap_sum(VALUE args)
{
 VALUE * values = (VALUE *) args;
 VALUE summer = values[0]; 
 VALUE max = values[1];
 return rb_funcall(summer,id_sum,1,max);
}
static VALUE protected_sum(VALUE summer, VALUE max)
{
 int error;  VALUE args[2];
 VALUE result;
 args[0] = summer;
 args[1] = max;
 result = rb_protect(wrap_sum,(VALUE)args, &error);
 return error ? Qnil:result;
}
int main()
{
 int value;
 int *next = Values;
 ruby_init();
 ruby_init_loadpath();
 ruby_script("embedded");
 rb_require("sum.rb");
 VALUE summer = rb_class_new_instance(0,0,rb_const_get(rb_cObject,rb_intern("Summer")));
 id_sum = rb_intern("sum");
 while (value = *next++) 
 {
  VALUE result = protected_sum(summer,INT2NUM(value)); 
  if (NIL_P(re ......

ruby c++

While looking for information on the subject, I looked into the ONLamp article Extending Ruby with C by Garrett Rooney, the Extending Ruby chapter in the Pickaxe, README.EXT (located at /usr/share/doc/ruby1.8-dev/README.EXT.gz on my Ubuntu system) and got some help from Kjetil.
The resulting file can be found here: wtmplist.c.
Disclaimer: I’m not resposible if you should destroy something with your newfound skills, nor am I responsible if you should suddenly get the urge to leave your wife/loved ones in favor of Ruby.
Table of Contents:
What to Extend?
How Do I Want the Interface to Be?
Get to it! #include <ruby.h>
Groundwork.
Making it Compile.
Collect the Data th Make Available.
Adding More Functionality.
Test Your New Library.
What to Extend?
Obviously you start by figuring out what you want to extend Ruby with. Some library missing, or better implemented in C? Some library you wrote yourself and want to be able to use from Ruby scripts?
I have a project c ......

ruby c++

While looking for information on the subject, I looked into the ONLamp article Extending Ruby with C by Garrett Rooney, the Extending Ruby chapter in the Pickaxe, README.EXT (located at /usr/share/doc/ruby1.8-dev/README.EXT.gz on my Ubuntu system) and got some help from Kjetil.
The resulting file can be found here: wtmplist.c.
Disclaimer: I’m not resposible if you should destroy something with your newfound skills, nor am I responsible if you should suddenly get the urge to leave your wife/loved ones in favor of Ruby.
Table of Contents:
What to Extend?
How Do I Want the Interface to Be?
Get to it! #include <ruby.h>
Groundwork.
Making it Compile.
Collect the Data th Make Available.
Adding More Functionality.
Test Your New Library.
What to Extend?
Obviously you start by figuring out what you want to extend Ruby with. Some library missing, or better implemented in C? Some library you wrote yourself and want to be able to use from Ruby scripts?
I have a project c ......
总记录数:146; 总页数:25; 每页6 条; 首页 上一页 [15] [16] [17] [18] 19 [20] [21] [22] [23] [24]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号