Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 5 - Iterating and Incrementing Strings in Ruby
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Iterating and Incrementing Strings in Ruby
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-08-21

    Table of Contents:
  • Iterating and Incrementing Strings in Ruby
  • Iterating Over a String
  • Managing Whitespace, etc.
  • Incrementing Strings
  • Converting Strings
  • Regular Expressions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Iterating and Incrementing Strings in Ruby - Converting Strings


    (Page 5 of 6 )

    You can convert a string into a float (Float) or integer (Fixnum). To convert a string into a float, or, more precisely, an instance of the Stringclass into an instance ofFloat, use theto_fmethod:

      "200".class # => String
      "200".to_f # => 200.0
      "200".to_f.class # => Float

    Likewise, to convert a string to an integer, useto_i:

      "100".class # => String
     
    "100".to_i # => 100
     
    "100".to_i.class # => Fixnum

    To convert a string into a symbol (Symbolclass), you can use either theto_symorinternmethods.

      "name".intern # => :name
     
    "name".to_sym # => :name

    The value of the string, not its name, becomes the symbol:

      play = "The Merchant of Venice".intern # => :"The Merchant of Venice"

    Convert an object to a string withto_s. Ruby calls theto_smethod from the class of the object, not theStringclass (parentheses are optional).

      (256.0).class # => Float
     
    (256.0).to_s # => "256.0"

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning Ruby," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter four of Learning Ruby, written by Michael Fitzgerald (O'Reilly; ISBN: 0596529864). Check it out today at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT