Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Fun with Strings
#1
Hey there.

Today I'm going to tell you about strings, and the fun things you can do with them. First, I'll try to explain what a string is by giving some examples and a quote from Google.

Google Wrote:a collection of objects threaded on a single strand
I know the above quote is to do with things irl, but it's pretty much the same for Programming in Ruby. A string simply is a line of code with objects.

Example:
"This is a string"

1. Reversing a String.
There's a simple way to reverse a string, printing it out backwards in Ruby. For example, here is a code that gets the User's Number / Word and prints it out reversed.
Code:
############################################################################
#    Copyright (C) 2009 by Pyrite Software Developers                                                  
#                                                                                                                                
#                                                                                                                                
#    This program is free software; you can redistribute it and#or modify                        
#    it under the terms of the GNU General Public License as published by                    
#    the Free Software Foundation; either version 2 of the License, or                            
#    (at your option) any later version.                                                                          
#                                                                                                                              
#    This program is distributed in the hope that it will be useful,                                  
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        
#    GNU General Public License for more details.                          
#                                                                          
#    You should have received a copy of the GNU General Public License    
#    along with this program; if not, write to the                        
#    Free Software Foundation, Inc.,                                      
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            
############################################################################

#! /usr/bin/env ruby

puts "Enter a Number or Word: "
num = gets.chomp
puts ""

puts "This is your number / word reversed: "
print num.reverse
puts ""

Really the only code in there you may or may not know is the num.reverse. Each time a User inputs sometihng it is automatically put into what we call strings. If we were to write:
Code:
puts "Enter a Number or Word: "
num = gets.chomp

print num.reverse
This is telling the program to Reverse the string that the user has entered. Say for example I write "Jordan" (Without the quotation marks) the program would recognise it as a string and print back "nodraJ".

Here's my example from the program I made:
My Script Wrote:jordan@ModernWarfare ~/Desktop/Ruby $ ruby strings.rb
Enter a Number or Word:
132

This is your number / word reversed:
231
jordan@ModernWarfare ~/Desktop/Ruby $
jordan@ModernWarfare ~/Desktop/Ruby $ ruby strings.rb
Enter a Number or Word:
Jordan

This is your number / word reversed:
nadroJ

2. Uppcasing and Lowercasing
Doing an upper case and a lower case is no different to doing a reverse. Just simple replace .reverse with either .upcase or .lowcase

Example:
Code:
puts "Enter a Word: "
num = gets.chomp

print num.upcase
Would make the output "JORDAN" or whatever your word was.
Where as:
Code:
puts "Enter a Word: "
num = gets.chomp

print num.lowcase
Would make the output from "JORDAN" to "jordan".

Thanks for reading my tut on fun with Strings, and I hope it helped you learn something.
Thanks!
-Jordan.
At the top will be the same place you hang from.
Reply


Messages In This Thread
[Tut] Fun with Strings - by Jordan L. - 01-14-2010, 06:53 PM
RE: [Tut] Fun with Strings - by Gaijin - 01-14-2010, 07:07 PM
RE: [Tut] Fun with Strings - by Jordan L. - 01-14-2010, 07:44 PM
RE: [Tut] Fun with Strings - by Gaijin - 01-14-2010, 07:55 PM
RE: [Tut] Fun with Strings - by Jordan L. - 01-14-2010, 08:07 PM
RE: [Tut] Fun with Strings - by Gaijin - 01-14-2010, 08:11 PM
RE: [Tut] Fun with Strings - by Jordan L. - 01-14-2010, 08:14 PM
RE: [Tut] Fun with Strings - by Gaijin - 01-17-2010, 06:34 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)