SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
Thread: My first Ruby Program
-
Jul 10, 2006, 11:47 #1
- Join Date
- Jun 2006
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Jul 10, 2006, 13:21 #2
- 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.
-
Jul 10, 2006, 14:17 #3
- 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 ;-).
-
Jul 10, 2006, 16:01 #4
- Join Date
- Jun 2006
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
What is the difference.
-
Jul 10, 2006, 16:10 #5
- Join Date
- Dec 2005
- Posts
- 0
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
Good on you bud, keep up the learning.
-
Jul 11, 2006, 00:43 #6
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by GafferSports
One little thing though:
Code:nfh = File.new("names.out", "w") # this creates the log file. nfh.puts name nfh.close
Code:File.open("names.out", "w").write(name)
-
Jul 11, 2006, 04:09 #7
- Join Date
- Jun 2006
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
-
Jul 11, 2006, 06:09 #8
- 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.
-
Jul 12, 2006, 09:44 #9
- Join Date
- Jun 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
-
Jul 12, 2006, 10:02 #10
- 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]
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.
-
Jul 12, 2006, 12:38 #11
- Join Date
- Jun 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?
-
Jul 12, 2006, 12:58 #12
- 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