IT Community - Software Programming, Web Development and Technical Support

Ruby on Rails

This is a discussion on Ruby on Rails within the Ruby forums, part of the Web Development category; Installing Ruby and Rails The installation process of Ruby and Rail on Windows, MAC and Linux are discussed detail in ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-09-2007, 01:48 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Installing Ruby and Rails

The installation process of Ruby and Rail on Windows, MAC and Linux are discussed detail in the following thread.

http://www.discussweb.com/ruby/4012-...s-windows.html

thanks
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-09-2007, 01:51 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Getting Started with Ruby

Ruby is the language that is going to make everything happen. To work with Ruby, you need a text editor of the kind you already used to get the Ruby on Rails example working, such as
word pad (Start➪ Programs➪Accessories➪WordPad) or
Notepad (Start➪Programs➪Accessories➪Notepad) in Windows.

Each Ruby program should be saved with the extension .rb
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-09-2007, 01:55 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Hello World in Ruby

To get started with Ruby and make it display a Hello World message, follow these steps:
  1. Start your text editor and enter the following Ruby code:

    Code:
    puts “Hello World.”
  2. Save the file as hello.rb. Make sure you save the file as a text file (select Text Document in the Save As Type drop-down), and if you are using windows WordPad or Notepad, make sure you enclose the name of the file in quotes—”hello.rb”—before saving to prevent those editors from saving the file as hello.rb.rtf or hello.rb.txt.

  3. Use Ruby to run this new program and see the results. Just enter the ruby command followed by the name of the program at the command line:

C:\rubydev>ruby hello.rb

Hello World.
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-09-2007, 01:57 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Working with Numbers in RUBY

To get started with numbers in Ruby, follow these steps:
  1. Enter this Ruby code in a new file:
    Code:
    puts 12345
    puts 3.1415
    puts 31415.0e-4
    puts 12_345_678_987_654_321
    puts 0xddff
  2. Save the file as numbers.rb.
  3. Use Ruby to run numbers.rb:

Quote:
C:\rubydev>ruby numbers.rb
12345
3.1415
3.1415
12345678987654321
56831
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-09-2007, 02:00 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Working with Strings in Ruby

To get started with strings in Ruby, follow these steps:
  1. Enter this Ruby code in a new file, strings.rb:


    Code:
    puts “Hello”
    puts “Hello “ + “there”
    puts ‘Nice to see you.’
    puts %Q/How are you?/
    puts %Q!Fine, and you?!
    puts %q!I’m also fine, thanks.!
    puts “I have to say, ‘I am well.’”
    puts “I’ll also say, \”Things are fine.\””
  2. Save the file as strings.rb.
  3. Run strings.rb using Ruby to see the result:

Quote:
C:\rubydev>ruby strings.rb
Hello
Hello there
Nice to see you.
How are you?
Fine, and you?
I’m also fine, thanks.
I have to say, ‘I am well.’
I’ll also say, “Things are fine.”
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-09-2007, 02:04 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Ruby’s reserved words:
  1. __FILE
  2. __ def
  3. in
  4. self
  5. __LINE
  6. __ defined?
  7. module
  8. super
  9. BEGIN
  10. do
  11. next
  12. then
  13. END
  14. else
  15. nil
  16. true
  17. alias
  18. elsif
  19. not
  20. undef
  21. and
  22. end
  23. or
  24. unless
  25. begin
  26. ensure
  27. redo
  28. until
  29. break
  30. false
  31. rescue
  32. when
  33. case
  34. for
  35. retry
  36. while
  37. class
  38. if
  39. return
  40. yield
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-09-2007, 02:07 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Default Ruby on Rails

Storing Data in Variables - Working with Variables

To get started with variables in Ruby, follow these steps:
  1. Enter this Ruby code in a new file, variables.rb:

    Code:
    temperature = 36
    puts “The temperature is “ + String(temperature) + “.”
    temperature = temperature + 5
    puts “Now the temperature is “ + String(temperature) + “.”
  2. Save the variables.rb file.
  3. Run variables.rb to see the result:
Quote:
C:\rubydev>ruby variables.rb
The temperature is 36.
Now the temperature is 41.
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-09-2007, 02:14 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Creating Constants

You can also create constants in Ruby. A constant holds a value that you do not expect to change, such as

the value of pi:

PI = 3.1415926535


IP_SERVER_SOURCE = “903.111.333.055”
IP_SERVER_TARGET = “903.111.333.056”


Unlike other languages, Ruby allows you to change the values in constants by assigning a new value to
them:
CONST = 1
CONST = 2

However, you’ll get a warning each time you do this:

constants.rb:2: warning: already initialized constant CONST
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-09-2007, 02:16 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Interpolating Variables


To get started with interpolating expressions in double-quoted strings in Ruby, follow these steps:
  1. Enter this code in a new file, doublequoted.rb:

    Quote:
    temperature = 36
    puts “The temperature is #{temperature}.”
    temperature = temperature + 5
    puts “Now the temperature is #{temperature}.”
  2. Save doublequoted.rb.
  3. Run doublequoted.rb:
Quote:
C:\rubydev>ruby doublequoted.rb
The temperature is 36.
Now the temperature is 41.
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-09-2007, 02:19 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Reading Text


To get started reading text in Ruby, follow these steps:
  1. Enter this code in a new file, gets.rb:
    Quote:
    print “Please enter the temperature: “
    gets
    chomp
    puts “The temperature is #{$_}.”
  2. Save the file and run it.
  3. Ruby displays the prompt Please enter the temperature: and then waits for a response from you:
    Quote:
    C:\rubydev>ruby gets.rb
    Please enter the temperature:
  4. Enter a temperature and press Enter. Ruby reads the text that you have entered, chomps the
    newline character off the end of it, and displays the resulting text:
Quote:
C:\rubydev>ruby gets.rb
Please enter the temperature: 36
The temperature is 36.
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 10-09-2007, 02:22 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Using Operators

To get started using operators in Ruby, follow these steps:
  1. Enter this code in a new file, operators.rb:

    Quote:
    value = 3
    puts value
    value = value + 3 #addition
    puts value
    value = value / 2 #division
    puts value
    value = value * 3 #multiplication
    puts value
    value = value ** 2 #exponentiation
    puts value
  2. Save operators.rb.
  3. Run operators.rb:
Quote:
C:\rubydev>ruby operators.rb
3
6
3
9
81
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 10-09-2007, 02:28 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Ruby Operators

Ruby Operators Operator Description
[ ] Array reference
[ ]= Array element set
** Exponentiation
! Not
~ Complement
+ Unary plus
- Minus
* Multiply
/ Divide
% Modulo
+ Plus
- Minus
>> Right shift
<< Left shift
& Bitwise And
^ Bitwise exclusive Or (Xor)
| Regular Or

Comparison operators:

<= Less than or equal to
< Less than
> Greater than
>= Greater than or equal to

Equality and pattern match operators:

<=> Less than, equal to, greater than
== Equal to
=== Tests equality in a when clause of a case statement
!= Not equal to
=~ Regular expression pattern match
&& Logical And
|| Logical Or
.. Inclusive range
... Exclusive range
? Ternary if
: Else

Assignment

= Normal assign
%= Modulus and assign
/= Divide and assign
-= Subtract and assign
+= Add and assign
*= Multiply and assign
**= Exponent and assign
defined? True if symbol defined
not Logical negation
and Logical composition
or Logical composition

Statement modifiers

if
unless
while
until

begin/end Block expression
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 10-12-2007, 03:36 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,318
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up Ruby on Rails

Working with Arrays

There are plenty of ways to work with data in Ruby. Next up is to store it in arrays. Arrays act as groups of variables, and you can access each variable in an array with an index number.

For example, to create an array, you use the [] operator like this:

array = [1, 2, 3]

This creates an array with three elements, 1, 2, and 3. You access those elements using an array index like this:

Quote:
puts array[0] #prints 1
puts array[1] #prints 2
puts array[2] #prints 3
Note that the first element in the array corresponds to array index 0, not 1, and the second element is at index 1, the third at index 2, and so on. You can also assign values to arrays, using the array index:

Quote:
array[0] = 4 #assigns 4 to array[0]
array[1] = 5 #assigns 5 to array[1]
array[2] = 6 #assigns 6 to array[2]
puts array[2] #prints 6
As you know, Ruby variables can store text strings as well as numbers, and you can store both in arrays.

Here’s an example:

array = [“Hello”, “there”, “sweetie”, 1, 2, 3]

This creates an array filled with six elements, three of which are text and three of which are numbers:

Quote:
array = [“Hello”, “there”, “sweetie”, 1, 2, 3]
puts array[1] #prints “there”
puts array[4] #prints 2
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Ruby for Web? S.Vinothkumar Ruby 6 11-20-2007 01:25 AM
Installing Ruby and Rails on Windows prasannavigneshr Ruby 2 10-03-2007 03:11 AM
Ruby on Rails server config vivekanandan Ruby 7 08-04-2007 04:02 AM
setting up ruby on rails in Fedora core 6 rgm5 Ruby 1 04-17-2007 11:42 PM
Ruby IDE drecko Ruby 0 02-16-2007 12:11 AM


All times are GMT -7. The time now is 11:10 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0