Writing own Methods in Ruby  PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Ruby
Written by Adi Bach   
Friday, 16 February 2007

{mos_sb_discuss:50}

Let's look at writing one's own methods in Ruby with the help of a simple program mymethods.rb. Observe that we use def and end to declare a method. Parameters are simply a list of local variable names in parentheses.


                # A simple method

                def hello

                  puts 'Hello'

                end

                #use the method
                hello

                # Method with an argument - 1
                def hello1(name)
                 puts 'Hello ' + name
                 return 'success'
                end

                puts(hello1('satish'))

                # Method with an argument - 2
                def hello2 name2
                 puts 'Hello ' + name2
                 return 'success'
                end

                puts(hello2 'talim') # A method returns the value of the last line

Read more

  


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Friday, 16 February 2007 )
 
< Prev   Next >