. View this ad or post your own Ruby Job!

Monday, October 31, 2005

Some Methods in Ruby

What have I found out so far?
  • Definition of Ruby
  • Ruby Windows Installer
  • Interactive Ruby - Open a new DOS window and type irb --simple-prompt A prompt appears as >>
  • Run a Ruby program by typing ruby hello.rb
  • Learnt some features of Ruby like comments, naming rules, variables, constants, strings etc.
  • Various Text Editors/IDEs like SciTE, NotePad2
  • Constructs like the if else and while
Today, we shall explore some methods in Ruby. So far we had seen a method like puts that writes to the screen. How does one accept user input? For this gets and chomp are useful. The example below illustrates the same.
# gets and chomp
puts "In which city do you stay?"
city = gets.chomp
puts "The city is " + city
chomp is a string method and gets returns back a string. You must have realised that gets returns a string and a '\n' character, while chomp removes this '\n'.

There are many methods in string like the reverse that gives a backwards version of a string. length that tells us the number of characters (including spaces) in the string. upcase changes every lowercase letter to uppercase, and downcase changes every uppercase letter to lowercase. swapcase switches the case of every letter in the string, and finally, capitalize is just like downcase, except that it switches the first character to uppercase (if it is a letter).

Update: (11:10 hrs) I came across another construct, the do end. I shall show an example of this when I talk about arrays.

First Post | Previous | Next

Technorati Tags:
Blogs linking to this article


Post a Comment

Links to this post:

Create a Link

<< Home

Valid XHTML 1.0!