Ruby笔记
1.数组遍历方法总结
array = (1..10).to_a
length = array.length
length.times do t
print "#{array[t]} "
end
puts "n"
length = array.length-1
for i in 0..length do
print "#{array[i]} "
end
puts "n"
for i in array do
print "#{i} "
end
puts "n"
array.each{x print x," "}
puts "n"
length = array.length
i = 0
while i< length do
print "#{array[i]} "
i = i+1
end
puts "n"
length = array.length
i = 0
until i==length do
print "#{array[i]} "
i += 1
end
puts "n"
array.each_index do i
print "#{array[i]} "
2.Ruby连接数据库
- mysql
require 'mysql'
begin
db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME, 'utf8')
db = Mysql.real_connect("127.0.0.1", "root", "123456", "test", 3306)
db.query("SET NAMES utf8")
db.query("drop table if exists tb_test")
db.query("create table tb_test (id int,
text LONGTEXT) ENGINE=MyISAM DEFAULT CHARSET=utf8")
db.query("insert into tb_test (id, text) values (
1,'first line'),(2,'second line')")
printf "%d rows were inserted\n",db.affected_rows
rslt = db.query("select text from tb_test")
while row = rslt.fetch_row do
puts row[0]
end
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
db.close if db
end
- redis
#!/bin/ruby
require 'redis'
def writeToFile(file,content)
fp = File.new(file,"a+")
if fp
fp.syswrite(content)
else
puts "..."
end
end
def connect(host)
redis = Redis.new(:host => host,:port => 6379)
redis.info.keys.each do |key|
puts "#{key}:\t"+redis.info["#{key}"]
end
end
connect("1.1.1.1")
- sqlite3
require 'sqlite3'
db = SQLite3::Database.new('test.db')
db.execute("create table test(
ID integet not null,
Username varchar(20) null,
Password varchar(64) null)")
db.execute("insert into test(ID.Username,Password)
values('0','admin','admin')")
db.execute("select * from test")
db.execute("update test set password='12345' where id=0")
3.Ruby socket
- 服务端:
require 'socket'
server = TCPServer.open('0.0.0.0', 8080)
loop do
Thread.start(server.accept) do |client|
begin
while true
puts "#{client.to_i} online"
data = client.read()
throw "empty" if data.empty?
#puts data.length
puts data
end
rescue Exception => e
puts "#{client.to_i} offline"
end
end
end
- 客户端:
require 'socket'
hostname = '127.0.0.1'
port = 8080
buf = 'test'
s = TCPSocket.open(hostname, port)
s.write buf
sleep(1)
s.close
4.ruby gem 文档服务
rubygems.org上的gem文档访问起来太慢了,其实gem本身就自带doc的功能
安装gem的时候会默认安装相应gem的doc,如果不想占用空间安装doc,则gem install XXX --no-doc 即可。
使用下列命令可以启动gem自带的文档:
gem server --port 1234
然后访问http://localhost:1234就可以查看相关的gem文档。
5.ruby改变控制台输出内容的颜色
puts "\033[1m前景色\033[0m\n"
puts " \033[30mBlack (30)\033[0m\n"
puts " \033[31mRed (31)\033[0m\n"
puts " \033[32mGreen (32)\033[0m\n"
puts " \033[33mYellow (33)\033[0m\n"
puts " \033[34mBlue (34)\033[0m\n"
puts " \033[35mMagenta (35)\033[0m\n"
puts " \033[36mCyan (36)\033[0m\n"
puts " \033[37mWhite (37)\033[0m\n"
puts ''
puts "\033[1m背景色\033[0m\n"
puts " \033[40m\033[37mBlack (40), White Text\033[0m\n"
puts " \033[41mRed (41)\033[0m\n"
puts " \033[42mGreen (42)\033[0m\n"
puts " \033[43mYellow (43)\033[0m\n"
puts " \033[44mBlue (44)\033[0m\n"
puts " \033[45mMagenta (45)\033[0m\n"
puts " \033[46mCyan (46)\033[0m\n"
puts " \033[47mWhite (47)\033[0m\n"
puts ''
puts "\033[1m其他\033[0m\n"
puts " Reset (0)"
puts " \033[1mBold (1)\033[0m\n"
puts " \033[4mUnderlined (4)\033[0m\n"
6.一些比较特别的包
Ruby json gem
https://rubygems.global.ssl.fastly.net/gems/json-1.8.3.gem
树莓派wiringpi gpio包
http://pi.gadgetoid.com/article/wiringpi-as-a-ruby-gem
7.安装rvm的正确姿势
参考自:http://rvm.io/rvm/install
首先添加gpg公钥:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
安装稳定版本的rvm
curl -sSL https://get.rvm.io | bash -s stable --ruby
8.解决kali2.0中RVM不能编译ruby-2.3.3
- 错误详情:
ruby-2.3.3 - #compiling.......................................................................-
Error running '__rvm_make -j4',
showing last 15 lines of /usr/local/rvm/log/1488041042_ruby-2.3.3/make.log
exts.mk:210: recipe for target 'ext/openssl/all' failed
make[1]: *** [ext/openssl/all] Error 2
make[1]: *** Waiting for unfinished jobs....
installing default nkf libraries
compiling objspace_dump.c
linking shared-object json/ext/generator.so
make[2]: Leaving directory '/usr/local/rvm/src/ruby-2.3.3/ext/json/generator'
linking shared-object objspace.so
make[2]: Leaving directory '/usr/local/rvm/src/ruby-2.3.3/ext/objspace'
linking shared-object nkf.so
make[2]: Leaving directory '/usr/local/rvm/src/ruby-2.3.3/ext/nkf'
make[1]: Leaving directory '/usr/local/rvm/src/ruby-2.3.3'
uncommon.mk:203: recipe for target 'build-ext' failed
make: *** [build-ext] Error 2
++ return 2
There has been an error while running make. Halting the installation.
查看/usr/local/rvm/log/1488041042_ruby-2.3.3/make.log发现是openssl版本过老导致的。
- 解决:
第一步:先安装用于rvm的openssl:
rvm pkg install openssl
第二步:编译安装ruby,指定openssl目录(我的是/usr/local/rvm/usr/)
rvm install ruby-2.3.3 --with-openssl-dir=/usr/local/rvm/usr/
9.设置Gems默认源为ruby-china
现在没有淘宝源了,只有ruby-china源
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
设置Bundler默认源为ruby-china:
bundle config mirror.https://rubygems.org https://gems.ruby-china.org
这样修改以后,即使Gemfile中指定了Source,也会用国内的源。
Ruby笔记的更多相关文章
- ruby 笔记
symbol 不能有- 'data-turbolinks-track' => true stop rails –s kill -INT $(cat tmp/pids/server.pid) cl ...
- 基于ruby的watir自动化测试 笔记二
基于ruby的watir自动化测试 笔记一的补充版,新增加了些特殊的控件捕获方法.还在更新中.... attribute_value 获取当前控件的属性 Value = ie.link(:id=> ...
- Ruby自学笔记(一)— 基本概况
之前一直想要多看看RESTful Service相关的东西,找到一本相关的书,但是里面的代码都是用Ruby写的,虽然知道编程语言都是类似的,但是看到一些陌生的语法,还是有些摸不着头脑,所以最近终于下定 ...
- Memcached 学习笔记(二)——ruby调用
Memcached 学习笔记(二)——ruby调用 上一节我们讲述了怎样安装memcached及memcached常用命令.这一节我们将通过ruby来调用memcached相关操作. 第一步,安装ru ...
- Ruby零碎笔记
Ruby零碎笔记 飞机上阅读pdf的笔记,因为不联网,内容不多而且比较零散,以tips的形式记录 tips 查看当前作用域的变量 puts local_variables ruby中方法传递参数时,括 ...
- [Ting's笔记Day4]将Ruby on Rails项目部署到Heroku
今天想笔记的是把自己写的Ruby on Rails项目部署(Deploy)到Heroku! Heroku是Salesforce公司旗下的云端服务商,支持多种程序语言像是Ruby,PHP,Python等 ...
- [Ting's笔记Day1] Ruby on Rails练习- MacOS安装篇
千里之行,始于足下.喊了要学Ruby on Rails好久,今天终于要来迈向第一步:安装了! 一开始学习新的事物,主要就是跟着这个网页所说的步骤step by step. 很喜欢这个网页的设计流程,透 ...
- Ruby学习笔记4: 动态web app的建立
Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...
- ruby语言学习笔记2
ruby学习笔记2 (摘自<ruby程序设计语言教程(中文版)>.pdf,全书25页) 1.同一个问题ruby有多个解决方案,途径 ruby之父:松本行弘(Matz),1993年创立 r ...
随机推荐
- PJzhang:从js文件中寻找子域名的SubDomainizer
猫宁!!! 有些专门针对企业客户的网站,可能没有供公开注册的页面,但是在js文件中可能会隐藏他们的注册接口,当然这也是一种安全风险,就像有些后台是一定不能不小心放外网一个道理. 最近看到一篇文章提 ...
- java去除数组中的空值
public String[] deleteArrayNull(String []string) { String []array = string; // 声明一个list List<Stri ...
- 瀑布布局(waterflall flow)实现
瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动.这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pinterest,逐渐 ...
- 脚本自动创建ldap账号
背景:客服那边人员流动性大,经常需要配置账号,每次创建账号配置权限比较繁琐. 配置脚本: ldapadduser.sh #!/bin/bash # add ldap user ] || [[ $ -n ...
- VBnet窗口获取键盘输入
https://blog.csdn.net/youyoulg/article/details/39120669 C# WinFrom捕获按键按下事件(一) https://blog.csdn.net/ ...
- Minimum Score Triangulation of Polygon
Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in clockwi ...
- hive_UDTF函数
hive的UDTF函数是可以输入一行数据然后输出多行多列(可以是单行/单列)的函数 public class Tex extends GenericUDTF { /** * 对传入的参数进行初始化 * ...
- 封装CURD
<?php include ('ft.php'); $db=Danli::show(); //查询 //$re=$db->table('stree')->where(['name'= ...
- Https接口调用工具类
ClientUtil.java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org. ...
- C++ 对象构造顺序、构析函数、临时对象。
对象的构造顺序: 1.对于局部对象,构造顺序是根据程序执行流进行构造,从上到下. #include <stdio.h> class Test { int mi; public: Test( ...