![]() |
||
![]() |
![]() Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses. |
|
JAVA, JSP, SERVLETS, TOMCAT, SERVLETS MANAGER, |
•10 Tips for Killer Website Design •7 Sure shots ways to improve your website •Attracting Visitors and Improving Your Search Results •Chasing the Search Engines Algorithms •Crash Course in Getting a 1 Google Ranking •Design Your Site for Traffic in 2005 •Designing A Website That Sells •Googles Good Writing Content Filter •How to Write Effective Web Copy •How to Write Title Tags for Your Web Pages •JSP •JSP Scripting Elements and Variables •Java How to Send Email in Java •Java MySQL Database Connection •Make Money Fast With Google Adwords •Make Money On The Internet What Is Your Niche •Make Money Quick With Google Adsense •Ranked 1 at Google for Invisible Entrepreneurs But No Traffic •Ruby Classes Objects and Variables •Ruby Containers Blocks and Iterators •SEO One Way Web Links 5 Strategies •SEO Success Step Two - Attracting Search Engine Attention •The 10 Best Resources for CSS •The 3 Best Website Traffic Sources •The 5 Biggest Mistakes Almost All Web Designers Make •The Five Ways You Should Be Using Keywords •The Three Principles Of Image Optimization •Top 5 Secrets to Making Money with Adsense •True Paid Inclusion Programs are a Thing of the Past •Understanding Web Logs And Why it Matters •Temp
|
Web Hosting Tips for Webmasters - |
print "Enter your name: " name = gets |
Kernel
module---gets
, open
, print
,
printf
, putc
, puts
, readline
,
readlines
, and test
---that make it simple and
convenient to write straightforward Ruby programs. These methods
typically do I/O to standard input and standard output, which makes
them useful for writing filters. You'll find them documented starting
on page 411.
The second way, which gives you a lot more control, is to use IO
objects.
IO
, to handle input and output.
This base class is subclassed by classes File
and
BasicSocket
to provide more specialized behavior, but the
principles are the same throughout. An IO
object is a bidirectional
channel between a Ruby program and some external resource.[For those
who just have to know the implementation details, this means that a
single IO
object can sometimes be managing more than one operating
system file descriptor. For example, if you open a pair of pipes, a
single IO
object contains both a read pipe and a write pipe.]
There may be more to an IO
object than meets the eye, but in the
end you still simply write to it and read from it.
In this chapter, we'll be concentrating on class IO
and its most
commonly used subclass, class File
. For more details on using the
socket classes for networking, see the section beginning on page 469.
File.new
.
aFile = File.new("testfile", "r") # ... process the file aFile.close |
File
object that is open for reading, writing, or
both, according to the mode string (here we opened ``testfile
'' for
reading with an ``r
''). The full list of allowed modes appears
on page 326. You can also optionally specify file
permissions when creating a file; see the description of
File.new
on page 303 for details. After opening
the file, we can work with it, writing and/or reading data as needed.
Finally, as responsible software citizens, we close the file, ensuring
that all buffered data is written and that all related resources are
freed.
But here Ruby can make life a little bit easier for you. The method
File.open
also opens a file. In regular use, it behaves just
like
File.new
.
However, if there's a block associated with the
call, open
behaves differently. Instead of returning a new
File
object, it invokes the block, passing the
newly opened File
as a parameter. When the block exits, the file
is automatically closed.
File.open("testfile", "r") do |aFile| # ... process the file end |
gets
reads a line from standard
input, and aFile.gets
reads a line from the file object
aFile
.
However, I/O objects enjoy an additional set of access methods,
all intended to make our lives easier.
IO
stream,
you can also use various Ruby iterators.
IO#each_byte
invokes a
block with the next 8-bit byte from the IO
object (in this case,
an object of type File
).
aFile = File.new("testfile") aFile.each_byte {|ch| putc ch; putc ?. } |
T.h.i.s. .i.s. .l.i.n.e. .o.n.e. .T.h.i.s. .i.s. .l.i.n.e. .t.w.o. .T.h.i.s. .i.s. .l.i.n.e. .t.h.r.e.e. .A.n.d. .s.o. .o.n....... . |
IO#each_line
calls the block with the next line from the file.
In the next example, we'll make the original newlines visible using
String#dump
, so you can see that we're not cheating.
aFile.each_line {|line| puts "Got #{line.dump}" } |
Got "This is line one\n" Got "This is line two\n" Got "This is line three\n" Got "And so on...\n" |
each_line
any sequence of characters as a line
separator, and it will break up the input accordingly, returning
the line ending at the end of each line of data. That's why you see
the ``\n
'' characters in the output of the previous example.
In the next example, we'll use ``e
'' as the line separator.
aFile.each_line("e") do |line| puts "Got #{ line.dump }" end |
Got "This is line" Got " one" Got "\nThis is line" Got " two\nThis is line" Got " thre" Got "e" Got "\nAnd so on...\n" |
IO.foreach
. This method takes the name of an
I/O source, opens it for reading, calls the iterator once for every
line in the file, and then closes the file automatically.
IO.foreach("testfile") { |line| puts line } |
This is line one This is line two This is line three And so on... |
arr = IO.readlines("testfile")
|
||
arr.length
|
» |
4
|
arr[0]
|
» |
"This is line one\n"
|
puts
and print
,
passing in any old object and trusting that Ruby will do the right
thing (which, of course, it does). But what exactly is it doing?
The answer is pretty simple. With a couple of exceptions, every object
you pass to puts
and print
is converted to a string
by calling that object's to_s
method. If for some reason
the to_s
method doesn't return a valid string, a string is
created containing the object's class name and id, something like
<ClassName:0x123456>
.
The exceptions are simple, too. The nil
object will print as the
string ``nil,'' and an array passed to puts
will be written
as if each of its elements in turn were passed separately to
puts
.
What if you want to write binary data and don't want Ruby messing
with it?
Well, normally you can simply use
IO#print
and pass
in a string containing the bytes to be written. However, you can
get at the low-level input and output routines if you really
want---have a look at the documentation for
IO#sysread
and
IO#syswrite
on page 335.
And how do you get the binary data into a string in the first place?
The two common ways are to poke it in byte by byte or to use
Array#pack
.
str = ""
|
» |
""
|
str << 1 << 2 << 3
|
» |
"\001\002\003"
|
|
||
[ 4, 5, 6 ].pack("c*")
|
» |
"\004\005\006"
|
Array
using the
<<
operator, you can also append an object to an output IO
stream:
endl = "\n" $stdout << 99 << " red balloons" << endl |
99 red balloons |
<<
method uses to_s
to convert its
arguments to strings before sending them on their merry way.
require 'socket' client = TCPSocket.open('localhost', 'finger') client.send("oracle\n", 0) # 0 means standard packet puts client.readlines client.close |
Login: oracle Name: Oracle installation Directory: /home/oracle Shell: /bin/bash Never logged in. No Mail. No Plan. |
require 'net/http' h = Net::HTTP.new('www.pragmaticprogrammer.com', 80) resp, data = h.get('/index.html', nil) if resp.message == "OK" data.scan(/<img src="(.*?)"/) { |x| puts x } end |
images/title_main.gif images/dot.gif images/dot.gif images/dot.gif images/aafounders_70.jpg images/pp_cover_thumb.png images/ruby_cover_thumb.png images/dot.gif images/dot.gif |
Alden Hosting offers private JVM (Java Virtual Machine), Java Server Pages (JSP), Servlets, and Servlets Manager with our Web Hosting Plans WEB 4 PLAN and WEB 5 PLAN , WEB 6 PLAN .
At Alden Hosting we eat and breathe Java! We are the industry leader in providing affordable, quality and efficient Java web hosting in the shared hosting marketplace. All our sites run on our Java hosing platform configured for optimum performance using Java 1.6, Tomcat 6.0.X, MySQL 5.0.x, Apache 2.2.xx and web application frameworks such as Struts, Hibernate, Cocoon, Ant, etc.
We offer only one type of Java hosting - Private Tomcat. Hosting accounts on the Private Tomcat environment get their very own Tomcat server. You can start and re-start your entire Tomcat server yourself.
![]() |
|
http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP at JSP.aldenWEBhosting.com
Servlets at servlets.aldenWEBhosting.com
Tomcat at Tomcat.aldenWEBhosting.com
mysql at mysql.aldenWEBhosting.com
Java at Java.aldenWEBhosting.com
Web Hosts Portal
Web Links
Web Links JSP
Web Links servlet
Web Links
Web Links JSP
Web Links servlet
Web Hosting
JSP Solutions Web Links
JSP Solutions Web Hosting
Servlets Solutions Web Links
Servlets Solutions Web Hosting
Web Links
Web Links
.
.
.
.
.
.
.
.
.
.
jsp hosting
servlets hosting
web hosting
web sites designed
cheap web hosting
web site hosting
myspace web hosting