Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Ruby for the Newbie
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

Ruby for the Newbie
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-10-15

    Table of Contents:
  • Ruby for the Newbie
  • A Few Notes About Formatting
  • Variables
  • Constants

  • 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


    Ruby for the Newbie - Constants


    (Page 4 of 4 )

    Change is the one true constant. I never have bills, only change. It never changes. Ha ha. Seriously, constants in Ruby are similar to variables, except that they hold values you don't expect to change. This is different from most programming languages, where constants don't change.

    Unlike variables, the first letter of a constant must be uppercase (it's best just to make the whole name uppercase).

    Declaring a constant is the same as declaring a variable:

     

    SOCIAL= "193-12-1045"

    SOCIAL_SECURITY= "888-88-8888"

    Although you will get a warning message each time you change a constant, you can still do so:


    SOCIAL= "111-11-1111"

    This will result in a message that your file constants have already been initialized.

    However, the value will still be changed.

    Gathering Data from the User

    Sometimes you need to gather data from a user. Say you want to rob them blind. You would have to have them give you their pin number. You might also want to taunt them. I mean what good is stealing someone's money if you can't mock them afterwards?

    To do this, you will use the gets method.


    print "What is your pin number?"

    gets

    chomp

    puts "I R LEET. I HAV UR PIN #($_)!"

    You might notice a few new things in that code. As I said before, the gets method retrieves the data the user inputs, including the enter key they press to send the data. This value plus the enter key is stored in a default variable, $_. To get rid of the enter key, we use the chomp method. Finally, we print the final sentence, inserting the value of our variable (the pin number in this instance) into the sentence. Resulting in:

      "What is your pin number?"

      "I R LEET. I HAVE UR PIN # (whatever value the user put in) !"

    That concludes this portion of the Ruby beginner's tutorial. In the next part, we will work with operators, arrays, and conditionals. See you then.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Before I comment, I just want to give you guys a brief heads-up: For some reason the...
       · Great article, James Payne "Gorilla Lord!" :)A couple of suggestions:1) This...
       · Hi Farsheed,Thanks for the comments. The sites are presently being redesigned,...
       · Well frankly, I have absolutely no idea how or where to begin. I'm more of a...
       · Hey! I finally found a solution to my problem regarding working in Shell and...
       · Hey Farsheed,Thanks for posting the solution. I have to agree with you regarding...
       · Hello James!As you can probably see, I finally created an account! I figured it...
     

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek