. View this ad or post your own Ruby Job!

Monday, October 31, 2005

Arrays 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
  • Some methods in Ruby.

Today, I am going to look at arrays in Ruby. Arrays are best explained by the following example.
# Arrays
name = ['Satish', 'Talim', 'Ruby', 'Java']
puts name[0]
puts name[1]
puts name[2]
puts name[3]
# the next one outputs nil
puts name[4]
# we can add more elements too
name[4] = 'Pune'
puts name[4]
# we can add anything!
name[5] = 4.33
puts name[5]
# we can add an array to an array
name[6] = [1, 2, 3]
puts name[6]

# some methods on arrays
newarr = [45, 23, 1, 90]
puts newarr.sort
puts newarr.reverse
puts newarr.length

# iterator each - extracts each element into lang
languages = ['Pune', 'Mumbai', 'Bangalore']

languages.each do |lang|
puts 'I love ' + lang + '!'
puts 'Don\'t you?'
end


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

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

Simple Constructs 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
Today, we shall explore some simple constructs available in Ruby. The example below illustrates the if else construct.
# if else
var = 5
if var > 4
puts "variable is greater than 4"
puts "I can have multiple statements here"
if var == 5
puts "nested if else possible"
else
puts "too cool"
end
else
puts "variable is not greater than 5"
puts "I can have multiple statements here"
end
Some common conditional operators are: ==, != >=, <=, >, <

Loops like the while are available. Again, the example below illustrates the same.
# Loops
var = 0
while var < 10
puts var.to_s
var += 1
end

First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

Sunday, October 30, 2005

Text Editors and IDEs for 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.
Which text editor should I use for Ruby?
  1. In your Ruby installation there is a built-in text editor 'SciTE' in the ruby/scite folder. SciTE is a SCIntilla based Text Editor.
  2. Your Ruby installation also has FreeRIDE, a pure Ruby Integrated Development Environment.
  3. A Ruby Editor plugin for jEdit is also available, as a free download. This converts jEdit into an intelligent Ruby IDE.
Which one to use? I have asked this question to the Site Point's Ruby Forum and awaiting a reply. In the meantime, I too shall explore these software and decide which one to use.

Update: (18.30 hrs) I tried out SciTE and found it useful. I have not had time to evaluate the other two, so I guess I shall stick to SciTE. MiiJaySung on the Ruby forum has suggested NotePad2 for Ruby as a better alternative.


First Post | Previous | Next



Technorati Tags: , , , , ,
Blogs linking to this article

Wednesday, October 26, 2005

Ruby Nuts and Bolts

What have I found out so far? Now, let us explore some of the features of Ruby.

  1. Free format
  2. Case sensitive
  3. Comments - Anything following an unquoted #, to the end of the line on which it appears, is ignored by the interpreter. Also, to facilitate large comment blocks, the ruby interpreter also ignores anything between a line starting with "=begin" and another line starting with "=end"
  4. Statement delimiters - Multiple statements on one line must be separated by semicolons, but they are not required at the end of a line; a linefeed is treated like a semicolon. If a line ends with a backslash (\), the linefeed following it is ignored; this allows you to have a single logical line that spans several lines
  5. Names in Ruby - Ruby names are used to refer to constants, variables, methods, classes, and modules (more of this later). The first character of a name helps Ruby to distinguish its intended use. Certain names, are reserved words and should not be used as variable, method, class, or module names. Lowercase letter means the characters ''a'' though ''z'', as well as ''_'', the underscore. Uppercase letter means ''A'' though ''Z,'' and digit means ''0'' through ''9.'' Name characters means any combination of upper- and lowercase letters and digits
  6. Variables - Variables in Ruby can contain data of any type. You can use variables in your Ruby programs without any declarations. Variable name itself denotes its scope (local, global, instance, etc.).
    1. A local variable name consists of a lowercase letter followed by name characters (sunil, myCount, _z, hit_and_run).
    2. An instance variable name starts with an ''at'' sign (''@'') followed by an upper- or lowercase letter, optionally followed by name characters (@sign, @_, @Counter).
    3. A class variable name starts with two ''at'' signs (''@@'') followed by an upper- or lowercase letter. optionally followed by name characters (@@sign, @@_, @@Counter).
    4. A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. By convention, constant variables are normally spelled using uppercase letters and underscores throughout (module MyMath, PI=3.1416, class MyPune).
    5. Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x).
  7. Method names should begin with a lowercase letter. ''?'' and ''!'' are the only weird characters allowed as method name suffixes (More on this later).
  8. Strings have been explored in the program that follows:
=begin
Exploring Ruby
My first Ruby program!
=end

puts 1 + 2
puts 2 * 3
# Integer division
puts 3 / 2
puts 10 - 11
puts 1.5 / 2.6
puts "Hello World"
# Can use " or ' for Strings, but ' is more efficient
puts 'Hello World'
# Escape sequence
puts 'It\'s my Ruby'
# New here, displays the string three times
puts 'Hello' * 3
# String concatenation
puts "Hello" + "World"
# Defining a constant
PI = 3.1416
puts PI
# Defining a local variable
myString = "I love my city, Pune"
puts myString
=begin
Conversions
.to_i, .to_f, .to_s
=end
var1 = 5;
var2 = '2'
puts var1 + var2.to_i
It's to be noted that any given variable can at different times hold references to objects of many different types. A Ruby constant is also a reference to an object. Constants are created when they are first assigned to (normally in a class or module definition; they should not be defined in a method - more of this later). Ruby lets you alter the value of a constant, although this will generate a warning message.


More next time.


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

Monday, October 24, 2005

Ways To Run Ruby

What have I found out so far? Having come from a Java background, I thought these series of slides - 10 Things Every Java Programmer Should Know About Ruby, would be useful. I went through them but did not follow much, for now. I shall come back to this later.

So how do I get started with Ruby? An online version of the first edition of Programming Ruby - The Pragmatic Programmer's Guide is available. The second edition needs to be purchased (18th Nov. - Marsee Henon from O'Reilly has sent me a review copy of this book. Thank you Marsee.). I also realised that the first edition book is available in the Ruby software that I had installed earlier.

I opened the Programming Ruby - The Pragmatic Programmer's Guide. While going through this book, I came across this - "there are two ways to run Ruby - interactively and as a program."

When you installed your Ruby software, the System Environment Variable path is already set to point to the bin folder of Ruby. Interactive Ruby means using a DOS window. To do this open a new DOS window and type irb --simple-prompt A prompt appears as >> I can start using this as a simple calculator. If I type 3+4 and then press the enter key, I get 7. Numbers can be Integers or Floating-point numbers.I found out that the simple operators like + - * / ** % are available in Ruby. Typing exit or quit, closes the DOS window. You can also customize this shell.

Before getting into the syntax of the language, let us look at the other option ie. running a Ruby program. Also, it would be a good idea to create a folder somewhere to keep all of your programs. Make sure that when you save a program, you save it into this folder.
Now type the following in your text editor -
puts "Hello World"
Save the file as hello.rb in your Ruby source code folder. Open a DOS window and go to your Ruby source code folder. Then run the program by typing:
ruby hello.rb
Note: puts simply writes onto the screen whatever comes after it. puts really means put string.

Ruby is the interpreted language, so you don't have to recompile to execute the program written in Ruby.

That's it for now.


First Post | Previous | Next



Technorati Tags:
Blogs linking to this article

Saturday, October 22, 2005

Finding Ruby

As mentioned in my previous post, this blog will record my progress of learning Ruby and Ruby On Rails. I remembering reading somewhere that to use Ruby On Rails, one needs to know the Ruby programming language.

I started off by making a search for "Ruby Quick Start" on the Google search engine. This returned me a list of around 15 sites but unluckily I could not find anything that could get me quickly started on Ruby. I then searched for "Ruby" and amongst the 1000's of listed sites, I chose Ruby: Programmer's Best Friend and Ruby Central. My main problem was to try and figure out the official Ruby site. Update: (31st Oct.) I finally realised that this is the Official Ruby Home Page.

Bruce's article mentioned that Ruby, is the hottest emerging dynamic language. That's fine, but then what's Ruby. I read what seemed to be the official definition of Ruby, but what I liked was when it said "Ruby puts the fun back into programming!"

I wanted to get started with Ruby programming and so clicked on Install Ruby under Windows - A single download that contains everything you need to run Ruby under various Windows operating systems. The version I installed was 1.8.2. Update: (19th Nov.) Ruby releases with even subversion numbers - 1.6, 1.8, and so on - are stable, public releases.

With Ruby installed, I was now ready to get my hands wet with Ruby.

Update (12th Nov. '05) I found an interesting article that talks amongst other things about What is Ruby - Ruby is a pure object-oriented programming language with a super clean syntax that makes programming elegant and fun. Ruby successfully combines Smalltalk's conceptual elegance, Python's ease of use and learning, and Perl's pragmatism. Ruby was created in 1993 by a Japanese, Yukihiro Matsumoto, a.k.a "Matz", and has started to become popular worldwide in the past few years as more English language books and documentation have become available.


First Post | Previous | Next



Technorati Tags: ,
Blogs linking to this article

Initiation into Ruby

Today, I came across this article on OnJava - Technologies to Watch: A Look at Four That May Challenge Java's Development Dominance by Bruce A. Tate. Bruce Tate has an amazing track record when it comes to identifying successful technologies. He was one of the early developers who
  • identified the emergence of the Spring framework;
  • predicted the demise of EJB 2 technologies a full year before the EJB 3 expert group abandoned the older approaches.
In his new book Beyond Java, Bruce looks at languages and technologies that may challenge Java's dominance in some development niches. In the above mentioned article, Bruce covers four important emerging technologies -
  • Dynamic Languages - Ruby, is the hottest emerging dynamic language. Also, the exploding Ruby on Rails framework takes good advantage of the capabilities in Ruby to build one of the most productive application development frameworks in existence. The list of Java developers adopting Ruby is astounding.
  • Continuation Servers
  • Convention Over Configuration
  • Metaprogramming
Over the past 25 years, I have moved on from programming languages like Assembly, Fortran, BASIC, COBOL, C, C++, C# to Java. Bruce mentions that "Java has been an outstanding development language for the industry because it's brought a remarkable unity and attention to important standards where practically none existed before. But like all programming languages, Java will too fade in time." This triggered off a process in me that said, I better take a look at Ruby and Ruby On Rails.

This blog records my progress of learning Ruby and later Ruby On Rails.

Next Post


Technorati Tags:
Blogs linking to this article

Valid XHTML 1.0!