Difference between revisions of "Ruby"
(→User Input) |
(→Variables and Data Types) |
||
Line 17: | Line 17: | ||
== Variables and Data Types == | == Variables and Data Types == | ||
+ | |||
+ | ===Local=== | ||
+ | A local variable is a variable that can only be used within the block it was initialized in. It can be created by making an object that starts with | ||
+ | a lowercase letter or an underscore. | ||
+ | |||
+ | <pre> | ||
+ | >> foo = 'bar' | ||
+ | => "bar" | ||
+ | </pre> | ||
+ | |||
+ | ===Global=== | ||
+ | |||
+ | Global variables can be accessed from anywhere within the entire program. They can be created by prefixing your variable the the '$' symbol. | ||
+ | Editting the assignment of a global variable will change the status of that variable globally and is generally avoided when writing Ruby scripts. | ||
+ | <pre> | ||
+ | $woot = 1337 | ||
+ | => 1337 | ||
+ | </pre> | ||
+ | |||
+ | ===Instance=== | ||
+ | |||
+ | Instance variables begin with the '@' symbol. Creating an uninitialzed instance will have a nil value. | ||
+ | <pre> | ||
+ | >> @instance | ||
+ | => nil | ||
+ | >> @instance = 'ohdae' | ||
+ | => "ohdae" | ||
+ | </pre> | ||
+ | |||
+ | ===Class=== | ||
+ | Class variables are shared by all methods within that class. These are created by using two '@' symbols at the beginning of your variable. | ||
+ | Trying to initialize a class variable outside of a class will throw an error. | ||
+ | <pre> | ||
+ | >> @@classvar | ||
+ | NameError: uninitialized class variable @@classvar in Object | ||
+ | from (irb):5 | ||
+ | from :0 | ||
+ | >> class Blackhat | ||
+ | >> @@classvar = 'ohhai' | ||
+ | </pre> | ||
+ | |||
+ | ===Pre-defined=== | ||
+ | |||
+ | Certain variables are pre-defined into Ruby. The values of these variables cannot be changed. | ||
+ | |||
+ | <pre> | ||
+ | self | ||
+ | nil | ||
+ | true | ||
+ | false | ||
+ | </pre> | ||
+ | |||
+ | |||
=== Scalars === | === Scalars === | ||
=== Arrays === | === Arrays === | ||
Line 22: | Line 75: | ||
=== References and Pointers === | === References and Pointers === | ||
=== Casting === | === Casting === | ||
− | |||
== Boolean Logic == | == Boolean Logic == |
Revision as of 17:18, 12 August 2012
Ruby is an interpreted language, dynamically, reflective, semi-Functional and Object Orientated scripting language written in C. Ruby is said to be semi-Functional because it supports higher-order functions (aka lambdas) and closures (aka blocks). Ruby was created by Yukihiro "Matz" Matsumoto and was first released in 1995.
Matz's goal was to combine powerful features from various other programming languages, and create a programming language maximized for developer happiness; as opposed to computational efficiency. Ruby's Object Model mirrors that of Smalltalk, the syntax shares some similarities with Bash, Perl, Python, and the scoping rules for closures was taken from LISP.
Contents
Basics
Development Environment
Your first program
Code
#!/usr/bin/ruby puts "Hello world\n" |
Explanation
Variables and Data Types
Local
A local variable is a variable that can only be used within the block it was initialized in. It can be created by making an object that starts with a lowercase letter or an underscore.
>> foo = 'bar' => "bar"
Global
Global variables can be accessed from anywhere within the entire program. They can be created by prefixing your variable the the '$' symbol. Editting the assignment of a global variable will change the status of that variable globally and is generally avoided when writing Ruby scripts.
$woot = 1337 => 1337
Instance
Instance variables begin with the '@' symbol. Creating an uninitialzed instance will have a nil value.
>> @instance => nil >> @instance = 'ohdae' => "ohdae"
Class
Class variables are shared by all methods within that class. These are created by using two '@' symbols at the beginning of your variable. Trying to initialize a class variable outside of a class will throw an error.
>> @@classvar NameError: uninitialized class variable @@classvar in Object from (irb):5 from :0 >> class Blackhat >> @@classvar = 'ohhai'
Pre-defined
Certain variables are pre-defined into Ruby. The values of these variables cannot be changed.
self nil true false
Scalars
Arrays
Hashes or Associative Arrays
References and Pointers
Casting
Boolean Logic
Operators
Statements
Helper natives
Bitwise Manipulations
Loops
While
Until
For
Iterators
User Input
CGI
Command-line Options
STDIN / Standard Input
User-defined
Functions
Objects
Helpful Libraries
.....