Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Ruby, Variables and Functions

ruby

  • Please log in to reply
No replies to this topic

#1 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 August 2011 - 03:56 PM

In the last tutorial, we made a simple 'hello world' program; this time, let's do something with variables and functions.

Overview
  • Local Variables
  • Global Variables
  • Functions
  • Function Arguments
  • Example Program












Local Variables
Ruby variables don't have to be declared and can be defined whenever needed.

One thing to remember is that variables always start with a lower-case letter.

To define a local variable, with name variable_name, with value variable_value, you type:
[COLOR=#FF0000]variable_name[/COLOR]= [COLOR=#00FF00]variable_value[/COLOR]

The variable value can be a string, an integer (a whole positive or negative number), a float (a number with a decimal point), or another value.

To define an array, you can do something like this:
[COLOR=#FF0000]variable_name[/COLOR]= [[COLOR=#0000FF]element1_value[/COLOR], [COLOR=#0000FF]element2_value[/COLOR], ...]

When you have local variables, other functions can't access them; if you want (other) functions to have access to certain variables, you have to define those as global variables.








Global Variables
Global variables are defined in a similar manner to local variables, except that you need a '$' sign in front of the variable name, whenever you define or access the variable.

Once again, variable names don't start with capital letters.

To define a global variable with name name and value value, you type:
$[COLOR=#FF0000]name[/COLOR]= [COLOR=#00FF00]value[/COLOR]

So basically, you define and use global variables the same way you would with local variables, except that you need an extra leading dollar sign ($) whenever you reference $global variables.









Functions
You can define and redefine functions (or methods) whenever you want; when you call a function, just like with variables, you'll get whatever you defined last under that name.

For example, you define function f to return 1. When you call f, it'll return 1. Then you redefine f to return 2. Now, whenever you call that function, it'll return 2.

But it won't work if you call a function that you didn't yet define, so you need to define the function before you call it.

To define a function, named name, you type:
def [COLOR=#FF0000]name[/COLOR] 
    ...the code to run when the function is called...  
end

'def' tells ruby that it's a function definition. 'end' tells ruby that it's the end of the definition; the keyword 'end' is also used in a lot of other things, such as conditional (if) statements.

In other languages, such as JavaScript, you use '}'; in Ruby, you use 'end' .








Function Arguments
For functions to have arguments, you just have to add the parameter (argument) names right after the function name in the function definition.

For example, if you have two arguments, arg1 and arg2, you would use:
def [COLOR=#FF0000]name[/COLOR] [COLOR=#8888FF]arg1[/COLOR], [COLOR=#887711]arg2[/COLOR] 
    ...the code to run when the function is called...  
end

So to access those function arguments, all you have to do is type the argument variable name wherever you need it.

Default Argument Values
You can assign default argument values when you define a function. Let's say function name takes one argument, arg, and if that's left blank when the function's called, that value should be value; we would define the function like this:
def [COLOR=#FF0000]name[/COLOR] [COLOR=#8888FF]arg[/COLOR]= [COLOR=#4444FF]value[/COLOR] 
    ...the code to run when the function is called...  
end

So if you call name without providing any arguments, then arg would evaluate to value. If you do provide the argument then arg will evaluate to whatever you provide for the argument.














Example Program
Okay, let's put everything (or almost everything) together and make an example program; however, we won't use global variables this time, but those aren't very different from local variables - you need a leading '$' for the names, and you can access them from other functions - and I hope you understood how those work.

The Code
def my_function 
	# Set my_var to "I changed your variable. " 
	my_var= "I changed your variable. " 
	# Output my_var 
	puts my_var 
	# End of function. The function returns whatever was last 
	# in it; in this case, we return whatever puts() returned. 
	# puts() returns nil (the Ruby word for NULL), always. 
end 

def say_hello the_name 
	# Output the_name 
	puts "Hello #{the_name}!" 
end 

# Set my_var to "You can't change my variable. " 
my_var= "You can't change my variable. " 
# Call my_function() 
my_function 
# Output my_var 
puts my_var 

# Say hello to user1 
say_hello 'user1' 
# Say hello to Bob 
say_hello 'Bob' 

# But we can't just call say_hello with no arguments. Why not? 
# We'll need to redefine say_hello() for that. 
def say_hello the_name="World" 
	# Output the_name 
	puts "Hello #{the_name}!" 
end 

# Say hello to user25 
say_hello 'user25' 
# Say hello to 4 
say_hello 4 
# Say hello to the default name (which we defined to "World" , when we redefined say_hello()) 
say_hello 

# Wait for return key press. 
gets

The Output
Posted Image

(Fullsize Screenshot)














First Tutorial:
Hello World Introduction

Previous Tutorial:
Hello World Introduction

Next Tutorial:
Classes

Edited by RhetoricalRuvim, 27 August 2011 - 04:33 PM.

  • 0





Also tagged with one or more of these keywords: ruby

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download