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
#2
Nice one man, really good job!
Do you know something about Regular Expressions? Or how to replace parts of strings.
Reply
#3
I'm not sure about regular expressions, but I heard my mate is making a tut. Tongue
And for replacing in a strign, I think it's something using GSUB.
Like;
Code:
puts "Jordan".gsub("J", "X")
Would change J to X, making it Xordan.

I think that's how it works anyway, I don't know, try it. Mark (Wolskie) taught me that Smile
At the top will be the same place you hang from.
Reply
#4
(01-14-2010, 07:44 PM)trilobyte- Wrote: I'm not sure about regular expressions, but I heard my mate is making a tut. Tongue
And for replacing in a strign, I think it's something using GSUB.
Like;
Code:
puts "Jordan".gsub("J", "X")
Would change J to X, making it Xordan.

I think that's how it works anyway, I don't know, try it. Mark (Wolskie) taught me that Smile


Yes that worked, thanks!
And about the regular expressions, they work with the same function...

This code will convert every letter lowercase and uppercase from a-z with to the number 0...
Code:
puts "Jordan".gsub(/[A-Za-z]/, "0")
Reply
#5
Ah, I see what you mean.
Thanks for that, I didn't know that Tongue

So if I was to do:
Code:
puts "Jordan".gsub(/A]/, z)

Or sometihng like that, it would convert the capital J to a lowercase?
At the top will be the same place you hang from.
Reply
#6
(01-14-2010, 08:07 PM)trilobyte- Wrote: Ah, I see what you mean.
Thanks for that, I didn't know that Tongue

So if I was to do:
Code:
puts "Jordan".gsub(/A]/, z)

Or sometihng like that, it would convert the capital J to a lowercase?

You would use gsub(/[J]/, "j") to do that...
Your buddy has posted a tut, go and learn it Big Grin
Reply
#7
Will do, thanks MoTU. Smile
At the top will be the same place you hang from.
Reply
#8
I'm back with a question....
How can I use defined variables inside of a String?

Something like this in PHP:
PHP Code:
echo $hello." World";
echo 
"{$hello} World"
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)