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.
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.
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:
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:
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:
Would make the output "JORDAN" or whatever your word was.
Where as:
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.
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 strandI 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
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
Where as:
Code:
puts "Enter a Word: "
num = gets.chomp
print num.lowcase
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.