Ruby/Functions built in functions

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Ruby Functions and built in functions

A function is "a portion of code within a larger program, performs a specific task".

Functions have many benefits including:

  1. reducing the duplication of code in a program (e.g., by replicating useful functionality, such as mathematical functions);
  2. enabling reuse of code across multiple programs;
  3. decomposing complex problems into simpler pieces (this improves maintainability and ease of extension);
  4. improving readability of a program;
  5. hiding or regulating part of the program.
  • quoted from Wikipedia: practically, any program, no matter of the programming language used will increase in quality and functionality if you learn how to master functions.

I recommend you to also read this article on Wikipedia regarding functions if you would like to get a more advanced understanding of what they are and how to use them. The basic concepts are the same, even if that article hasn't been written specifically for Ruby.

You will learn more Ruby specifics as you keep reading this tutorial and also learn how to define your own functions when you don't find an equivalent in the ones already built in.

Functions are also know under many other names such as subroutines, methods or subprograms. Don't be surprised if you notice this tutorial has a chapter about methods that resemble the concepts of functions... because they actually are functions. It's easy to get confused with the terminology, but once you have some basic programming skills things will get much easier.


A list with some built in functions in Ruby

abort  
Array  
at_exit {...}  
binding   
block_given?   
callcc {| c|...}   
caller([ n])   
catch( tag) {...}   
chomp([ rs=$/])   
chomp!([ rs=$/])   
chop   
chop!   
eval( str[, scope[, file, line]])   
exec( cmd[, arg...])   
exit([ result=0])   
exit!([ result=0])   
fail(...)   
Float( obj)   
fork   
fork {...}   
format( fmt[, arg...])   
gets([ rs=$/])   
global_variables  
gsub( x, y)   
gsub( x) {...}   
gsub!( x, y)    
gsub!( x) {...}   
Integer( obj)   
lambda {| x|...}   
proc {| x|...}   
lambda   
proc 
load( file[, private=false])   
local_variables   
loop {...}   
open( path[, mode="r"])   
open( path[, mode="r"]) {| f|...}   
p( obj)   print([ arg...])   
printf( fmt[, arg...])   
proc {| x|...}   
putc( c)   
puts([ str])   
raise(...)   
fail(...)   
srand([ seed])   
String( obj)   
syscall( sys[, arg...])  
system( cmd[, arg...])   
sub( x, y)   
sub( x) {...}   
trap( sig, cmd)   
trap( sig) {...}   
untrace_var( var[, cmd])


Built in functions are functions that have already been defined for you and are included in the standard Ruby distribution. You use a built in function by calling it's name along with necessary parameters(if any required) like this:

puts "puts is a Ruby built in function"

You can find the syntax needed for using a certain function by using the fxri help console for example or by doing a search on Google. You can also find out more about what a certain function does by using the same methods above.

Image:Fxri_puts.gif

You now know the absolute basics of functions.If you also read the Wikipedia article I recommended, you now know A LOT about functions. All right, on with the next chapter!

Previous Next
Personal tools