View Full Version : آموزش: سورس کدهای آماده برای روبی
sajjad_india
یک شنبه 06 تیر 1389, 03:31 صبح
سلام به همه دوستان عزیز و گرامی
در ایت تاپیک قسط دارم که برای شما دوسان عزیز و گرامی سورس های آماده و کاربردی را در این محل قرار دهم .
همه میتونن در این تاپیک شرکت کنند . لطفا از عزیزانی سوالی چیزی نظری هرچی دارن پی ام بدن تاپیک رو بزارید به کار خودش ادامه بده
دوسنان تازه واردی که در مورد روبی چیزی نمیدونن و میخوان یاد بگیرند از پایه به تایپ من در اینجا (http://barnamenevis.org/forum/showthread.php?t=205442)مراجعه نمایند .
نکته : Sajjad_india من خودم هستم . پس نگران نباشید .
مممنون از همه شدوستان عزیز و گرامی
sajjad_india
یک شنبه 06 تیر 1389, 03:42 صبح
سورس اولی فایلی را باز نموده و در آن مینویسیم با دستور gets که خودمان در آن مینویسیم به صورت دستی
File.open('BarnamehNevis.txt', 'r') do |x|
while line = x.gets
puts line
end
end در این سورس هم ما یک فایل جدید ساخته و در آن یک خط مینویسیم مینویسیم .
File.open('MyTextFile.txt', 'w') do |y|
y.puts "Created by SAJJAD"
end
sajjad_india
یک شنبه 06 تیر 1389, 14:10 عصر
این دو تا سورس :
1- نشون دادن دایرکتوری های یک سایت
2- باز نمودن داریکتوری های تو در تو
تشکر یادت نره:چشمک:
sajjad_india
یک شنبه 06 تیر 1389, 14:23 عصر
سلام
روبی بانک های زیر رو پشتیبانی میکنه :
ADO (ActiveX Data Objects)
DB2
Frontbase
mSQL
MySQL
ODBC
Oracle
OCI8 (Oracle)
PostgreSQL
Proxy/Server
SQLite
SQLRelay
پس میبنید برنامه کاملا حرفه ایی و کاملی هستش از همه نظر
برای نصب نمون پک بانک اطلاعاتی از کد زیر در cmd استفاده کنید :
قدم اول :
$ tar zxf dbi-0.2.0.tar.gz قدم دوم :
$ ruby setup.rb config قدم سوم :
$ ruby setup.rb config --with=dbi,dbd_mysql
اتصال به بانک MySQL سرور
کد مذکور برای اتصال به بانک میباشد :
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
خروجی :
Server version: 5.0.45کد زیر هم برای وارد نمودن کدهایSQL در برنامه و اجرای آن در سرور
کد :
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
dbh.do( "INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME,
AGE,
SEX,
INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)" )
puts "Record has been created"
dbh.commit
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
dbh.rollback
ensure
# disconnect from server
dbh.disconnect if dbh
end
کد زیر هم برای وارد نمودن اطلاعات در بانکی که با دستورات SQL در کد قبلی درست کردیم
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
sth = dbh.prepare( "INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME,
AGE,
SEX,
INCOME)
VALUES (?, ?, ?, ?, ?)" )
sth.execute('John', 'Poul', 25, 'M', 2300)
sth.execute('Zara', 'Ali', 17, 'F', 1000)
sth.finish
dbh.commit
puts "Record has been created"
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
dbh.rollback
ensure
# disconnect from server
dbh.disconnect if dbh
end
کد زیر هم برای خواندن ریکورد ها و اطلاعات داخل Table :
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
sth = dbh.prepare("SELECT * FROM EMPLOYEE
WHERE INCOME > ?")
sth.execute(1000)
sth.fetch do |row|
printf "First Name: %s, Last Name : %s\n", row[0], row[1]
printf "Age: %d, Sex : %s\n", row[2], row[3]
printf "Salary :%d \n\n", row[4]
end
sth.finish
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
خروجی کد بالا :
First Name: Mac, Last Name : Mohan
Age: 20, Sex : M
Salary :2000
First Name: John, Last Name : Poul
Age: 25, Sex : M
Salary :2300
کد زیر هم برای آپدیت در بانک میباشد :
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
sth = dbh.prepare("UPDATE EMPLOYEE SET AGE = AGE + 1
WHERE SEX = ?")
sth.execute('M')
sth.finish
dbh.commit
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
dbh.rollback
ensure
# disconnect from server
dbh.disconnect if dbh
end
این کد هم برای Delete از بانک اطلاعاتی :
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
sth = dbh.prepare("DELETE FROM EMPLOYEE
WHERE AGE > ?")
sth.execute(20)
sth.finish
dbh.commit
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
dbh.rollback
ensure
# disconnect from server
dbh.disconnect if dbh
end
گرفتن اطلاعات از هاست و بانک روی هاست :
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"testuser", "test123")
puts dbh.func(:client_info)
puts dbh.func(:client_version)
puts dbh.func(:host_info)
puts dbh.func(:proto_info)
puts dbh.func(:server_info)
puts dbh.func(:thread_id)
puts dbh.func(:stat)
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
dbh.disconnect if dbh
end
sajjad_india
یک شنبه 06 تیر 1389, 14:26 عصر
تشکر یادت نره :
اینم از کد :
#!/usr/bin/ruby -w
time = Time.new
puts time.to_s
puts time.ctime
puts time.localtime
puts time.strftime("%Y-%m-%d %H:%M:%S")
خروجی :
Mon Jun 02 12:35:19 -0700 2008
Mon Jun 2 12:35:19 2008
Mon Jun 02 12:35:19 -0700 2008
2008-06-02 12:35:19
sajjad_india
یک شنبه 06 تیر 1389, 14:29 عصر
کد :
x=1
if x > 2
puts "x is greater than 2"
elsif x <= 2 and x!=0
puts "x is 1"
else
puts "I can't guess the number"
end
خروجی :
x is 1
sajjad_india
یک شنبه 06 تیر 1389, 14:31 عصر
در اینجا هم دستورات ورودی و خروجی رو برای شما میزرام . واسه تمرین خوبه
کد :
val1 = "This is variable one"
val2 = "This is variable two"
puts val1
puts val2
خروجی :
This is variable one
This is variable two
دستورات خروجی :
کد :
puts "Enter a value :"
val = gets
puts val
خروجی :
Enter a value :
This is entered value
This is entered value
sajjad_india
یک شنبه 06 تیر 1389, 14:37 عصر
کد مذکور :
#!/usr/bin/ruby
$i = 0;
$num = 5;
while $i < $num do
puts("Inside the loop i = #$i" );
$i +=1;
end
خروجی :
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
کد :
#!/usr/bin/ruby
$i = 0;
$num = 5;
until $i > $num do
puts("Inside the loop i = #$i" );
$i +=1;
end
خروجی :
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5
کد :
for i in 0..5
puts "Value of local variable is #{i}"
end
خروجی :
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
کد :
(0..5).each do |i|
puts "Value of local variable is #{i}"
end
خروجی :
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
کد :
for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end
خروجی :
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
sajjad_india
یک شنبه 06 تیر 1389, 14:40 عصر
آرایه ها :
کد :
names = Array.new(4, "mac")
puts "#{names}"
خروجی :
macmacmacmac
بحث آرایه ها گسترده هستش : فقط در این مثالی زدم که متوجه شید چی به چیه !!!
در آموزش ها براتون بعد مفصلا آموزش میدم
sajjad_india
دوشنبه 07 تیر 1389, 02:49 صبح
این کد به آی پی و پورت نظر متصل شده و پیغامی رو به صورت لوکال ایجاد میکند
require 'socket' # Sockets are in standard library
hostname = 'localhost'
port = 2000
s = TCPSocket.open(host, port)
while line = s.gets # Read lines from the socket
puts line.chop # And print with platform line terminator
end
s.close # Close the socket when done
لود شدن اطلاعات و پیج در روبی :
require 'socket'
host = 'www.tutorialspoint.com' # The web server
port = 80 # Default HTTP port
path = "/index.htm" # The file we want
# This is the HTTP request we send to fetch a file
request = "GET #{path} HTTP/1.0\r\n\r\n"
socket = TCPSocket.open(host,port) # Connect to server
socket.print(request) # Send request
response = socket.read # Read complete response
# Split response at first blank line into headers and body
headers,body = response.split("\r\n\r\n", 2)
print body
خاوندن هدر فایل سایت یا هاست مورد نظر :
require 'net/http' # The library we need
host = 'www.tutorialspoint.com' # The web server
path = '/index.htm' # The file we want
http = Net::HTTP.new(host) # Create a connection
headers, body = http.get(path) # Request the file
if headers.code == "200" # Check the status code
print body
else
puts "#{headers.code} #{headers.message}"
end
ادامه خواهد داشت به زودی ......
sajjad_india
دوشنبه 07 تیر 1389, 02:53 صبح
در این سری از کد میپردازیم در مورد رشته ها در روبی :
خیلی جالب و کاربردی هستن توصیه مشود به دقت بررسی بشه .
کد:
#!/usr/bin/ruby
x, y, z = 12, 36, 72
puts "The value of x is #{ x }."
puts "The sum of x and y is #{ x + y }."
puts "The average was #{ (x + y + z)/3 }."
خروجی :
The value of x is 12.
The sum of x and y is 48.
The average was 40.
کد :
myStr = String.new("THIS IS TEST")
foo = myStr.downcase
puts "#{foo}"
خروجی :
this is test
در آینده ادامه خواهد داشت ....
sajjad_india
دوشنبه 07 تیر 1389, 02:59 صبح
فرستادن ایمیل با روبی :
کد :
require 'net/smtp'
message = <<MESSAGE_END
From: Private Person <me@fromdomain.com>
To: A Test User <test@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
MESSAGE_END
Net::SMTP.start('localhost') do |smtp|
smtp.send_message message, 'me@fromdomain.com',
'test@todomain.com'
end
فرستادن ایمیل با فایل برای فرد :
کد :
require 'net/smtp'
filename = "/tmp/test.txt"
# Read a file and encode it into base64 format
filecontent = File.read(filename)
encodedcontent = [filecontent].pack("m") # base64
marker = "AUNIQUEMARKER"
body =<<EOF
This is a test email to send an attachement.
EOF
# Define the main headers.
part1 =<<EOF
From: Private Person <me@fromdomain.net>
To: A Test User <test@todmain.com>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
EOF
# Define the message action
part2 =<<EOF
Content-Type: text/plain
Content-Transfer-Encoding:8bit
#{body}
--#{marker}
EOF
# Define the attachment section
part3 =<<EOF
Content-Type: multipart/mixed; name=\"#{filename}\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{filename}"
#{encodedcontent}
--#{marker}--
EOF
mailtext = part1 + part2 + part3
# Let's put our code in safe area
begin
Net::SMTP.start('localhost') do |smtp|
smtp.sendmail(mailtext, 'me@fromdomain.net',
['test@todmain.com'])
end
rescue Exception => e
print "Exception occured: " + e
end
sajjad_india
دوشنبه 07 تیر 1389, 14:24 عصر
طرز استفاده :
فایل نوشته شده را در کنار این فایل گذاشته و در مسیر cmd ویندوز به صورت زیر مینویسیم و اجرا میکنیم و بعد از اتمام فایل exe ساخته شده در کنار فایل قبلی ایجاد میشود .
c:> Ruby ocrasa.rb Filename.rbفایل ضمیمه شد :لبخندساده:
sajjad_india
پنج شنبه 17 تیر 1389, 14:38 عصر
تبدیل MD5 و SHALL به یک دیگر .... :اشتباه::کف:
require 'digest/md5'
require 'digest/sha1'
aFile = File.open "length7v.txt", "r"
aFile.each do |line|
md5 = Digest::MD5::hexdigest(line.chomp).reverse
sha1 = "bb" + Digest::SHA1::hexdigest(md5)[2..39]
puts "Testing #{line}"
if sha1 == "bbc4d9cb08ddbce591c28effcf6a6120005dc477"
puts "Found the pass : #{line}"
system "pause"
end
end
sajjad_india
پنج شنبه 17 تیر 1389, 14:42 عصر
نمایش دیالگ باکس در روبی
require 'Qt4'
require 'mainwindow.rb'
require 'logindialog.rb'
class MW < Qt::MainWindow
slots 'setfocus()'
def initialize parent=nil
super
@ui = Ui_MW.new
@ui.setupUi self
...
end
end
class LoginDL < Qt::Dialog
slots 'enter_click()'
def initialize parent=nil
super
@ui = Ui_LoginDL.new
@ui.setupUi self
...
end
...
end
if $0 == __FILE__
app = Qt::Application.new(ARGV)
$mainw = MW.new
$mainw.show
loginw=LoginDL.new
loginw.show
app.exec
end
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.