SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile My first Ruby Program

    Code:
    print "What is your name? "
    # This asks for the persons name.
    name = gets
    # This Grabs the persons name and assigns it to a variable.
    puts " Your Name Is "
    puts name
    # This tells the person there name.
    nfh = File.new("names.out", "w")
    # this creates the log file.
    nfh.puts name
    nfh.close
    # This puts there name in the log file that was created.
    print "What Is your email Address? "
    # this asks the user what there email is.
    email = gets
    # this gets the email address that they put, and assigns it to a variable.
    puts " Your email Address is "
    puts email
    # this tells them the email address that they put in.
    efh = File.new("email.out", "w")
    # This creates the log file.
    efh.puts email
    efh.close
    # This puts there email address in the log file.
    print "What is your Age? "
    # This asks for there Age.
    age = gets
    # This grabs there age, and assigns it to a variable.
    puts " You are "
    puts age
    puts " years of age "
    # This tells them there age.
    afh = File.new("age.out", "w")
    # This creates the log file.
    afh.puts age
    afh.close
    # this creates the log file.
    print "Do you want to know the answers you just typed? "
    # Asks them a question.
    answer = gets
    # Grabs the answer and assigns it to a variable
    puts "So your name Email and age are: "
    # Tells them something.
    print "Your name is: "
    print name
    # Tells them there name.
    print "Your email is: "
    print email
    # Tells them there email.
    print "Your age is: "
    print age
    # Tells them there age.

    It's my first program it may not be much but im happy
    What yall think :-P Haha

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, do you have a lot of console users?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should code in real Ruby style:

    ['name','email','age'].map{|i|print "What is your #{i}? " or [i,gets]}.map{|i|
    File.open(i[0]+'.out','w').write i[1] and puts "Your #{i[0]} is: #{i[1]}"}

    Beat that ;-).

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    You should code in real Ruby style:

    ['name','email','age'].map{|i|print "What is your #{i}? " or [i,gets]}.map{|i|
    File.open(i[0]+'.out','w').write i[1] and puts "Your #{i[0]} is: #{i[1]}"}

    Beat that ;-).
    That aint what the book told me What is the difference.

  5. #5
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    You should code in real Ruby style:

    ['name','email','age'].map{|i|print "What is your #{i}? " or [i,gets]}.map{|i|
    File.open(i[0]+'.out','w').write i[1] and puts "Your #{i[0]} is: #{i[1]}"}

    Beat that ;-).
    Don't try and discredit him, he is obviously learning and proud of his first achievement.

    Good on you bud, keep up the learning.

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GafferSports
    Don't try and discredit him, he is obviously learning and proud of his first achievement.

    Good on you bud, keep up the learning.
    Just a joke, I didn't try to discredit him: his script is good. I can't even read my own script anymore

    One little thing though:

    Code:
    nfh = File.new("names.out", "w")
    # this creates the log file.
    nfh.puts name
    nfh.close
    Can be:

    Code:
    File.open("names.out", "w").write(name)

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    Just a joke, I didn't try to discredit him: his script is good. I

    Can be:

    Code:
    File.open("names.out", "w").write(name)
    Wow, I have a question did stuff change? like when a new release came out. And is that why i do not have to normally do all of that stuff? but i just dont know how to do it that way that you are doing lol. I think i need to get a more up to date book

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My first Ruby script has long since bit the dust, but one last vestage of it remains: a command line menu class:

    Code:
    class Menu
      def initialize(opts, name)
        @opts = opts
        @name = name
        @sep = ') '
      end
      
      def display
        puts "\n#{@name} Choices:\n"
        (1..@opts.length).each { |i| puts "#{i}#{@sep}#{@opts[i-1]}\n" } 
      end
      
      def choose
        choice = 0
        while (choice.to_i < 1 || choice.to_i > @opts.length) do
          display
          print "Input #{@name} number (1-#{@opts.length}): "
          choice = gets
          raise SystemExit.new(status=0) if choice.downcase.slice(0,1) == 'q'
        end
        choice.to_i
      end
    end
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweatje that is your first Ruby script? Im going to run it and see exactly what it does This is really interesting. Im also going to be creating another thread, about rails as well, because im going to have a question lol.
    Well, If everyone still has there first ruby scripts go ahead and post them lol. I want to see them as well

  10. #10
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The whole idea was to make a quick menu for command line applications.

    Here is an exaple

    Code:
     require 'menu'
     phpfiles = Dir.new('.').to_a.find_all{|f| /php$/.match(f)}
     menu = Menu.new(phpfiles, 'Pick a PHP file')
     which = menu.choose
     puts "You selected "+phpfiles[which-1]
    This might product output like this (depending on what php files were in the current directory):

    Code:
    Pick a PHP file Choices:
    1) mtrcfcst.php
    2) mtrcfcst_setup.php
    3) frcstcmt.php
    Input Pick a PHP file number (1-3):
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im confused on something can you make a web application without rails? because i know that ruby has ERB where you can enbed ruby scripts in html. by doing
    <%=>include_whatever.rb<=%> etc. i think thats how i do it lol. Well, is that possible to make applications without rails? or do you need rails?

  12. #12
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $ cat .htaccess
    Options +ExecCGI
    AddHandler cgi-script .rb

    Code:
    #!/usr/bin/ruby
    print "Content-type: text/html\n\n"
    
    puts "<h1>hello world!</h1>"
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •