Thursday, June 29, 2006

Own methods in Ruby

Let's look at writing one's own methods in Ruby with the help of a simple program MyMethods.rb.
# 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 hello1 name2
puts 'Hello ' + name2
return 'success'
end
puts(hello1 'satish')


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

3 Comments:

Blogger Ashish Kulkarni said...

Hi Satish,

1. Are parameters passed by value or reference in Ruby?

2. Is there a way of specifying if you want it either way?

3. Like C++, does Ruby allow default values to be allocated to parameters?

4. Does Ruby allow polymorphic functions (functions that would behave differently for different number and types of parameters)

5. Does Ruby allow operator overriding?

6. Are parameters passed from left to right on the stack (C) or right to left (Pascal)?

7. Does ruby allow us to write functions that can accept variable number of parameters?

3:12 PM  
Blogger Ashish Kulkarni said...

Got an answer to 3.

Yes, Ruby does allow default values to be allocated to parameters.

9:14 PM  
Anonymous shanko said...

Hi Ashish,

1. Parameters are passed by reference in ruby.

2. No

3. Yes ... default values can be specified to the parameters starting from rightmost to leftmost

4. Ruby has no notion of polymorphic functions in the sense of C++ since variables/argumanets in Ruby are dynamically typed.

5. Yes, Ruby allows operator overriding. Do it with care.

6. left to right (Ruby is written in C).

7. Yes, Ruby has a splat operator (*) which allows passing variable arguments.

5:32 PM  

Post a Comment

Links to this post:

Create a Link

<< Home