Ruby笔记

1.数组遍历方法总结
(1)

1
array = (1..10).to_a 

(2)

1
2
3
4
5
length = array.length 
length.times do t
print "#{array[t]} "
end
puts "n"

(3)

1
2
3
4
5
length = array.length-1 
for i in 0..length do
print "#{array[i]} "
end
puts "n"

(4)

1
2
3
4
5
6
7
8
9
for i in array do 
print "#{i} "
end
puts "n"
```Ruby
(5)
```Ruby
array.each{x print x," "}
puts "n"

(6)

1
2
3
4
5
6
7
length = array.length 
i = 0
while i< length do
print "#{array[i]} "
i = i+1
end
puts "n"

(7)

1
2
3
4
5
6
7
length = array.length 
i = 0
until i==length do
print "#{array[i]} "
i += 1
end
puts "n"

(8)

1
2
array.each_index do i 
print "#{array[i]} "

2.Ruby连接数据库
(1).mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

(2).redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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")

(3).sqlite3

1
2
3
4
5
6
7
8
9
10
11
12
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
服务端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

客户端:

1
2
3
4
5
6
7
8
9
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上main的gem文档访问起来太慢了,其实装了gem的机器本身就自带doc的功能
使用下列命令可以启动gem自带的文档

1
gem server --port 1234

然后用浏览器访问http://localhost:1234就可以查看相关的gem文档。

5.ruby改变控制台输出内容的颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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公钥:

1
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

安装稳定版本的rvm

1
curl -sSL https://get.rvm.io | bash -s stable --ruby
渗透中使用ssh转发流量的四种姿势 zmap使用笔记
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×