Ruby First
Ruby basics
Check version
ruby -v
irb
irb is conversationa dialogue style ruby script.
Start
irb
irb is irb command in bash
Test
p "Hello World!"
End
irb>> exit
Type exit
Value, Constants
a = 1 print a + 1
If missing initialization, value is nil
We can assign any types of value in variable.
Method
def add(a,b) # def a + b end print add(1,2) # Run
You can define 1 line using ;
def add(a,b); a + b; end
Class
class Homuhomu # class def madoka # method end end homu = Homuhomu.new homu.madoka
Command Line Arguments
Ruby is an interpreter, command in bin(UNIX)
p "hello, Ruby" p ARGV[0] # Command Line Arguments p ENV["PATH"] # Env PATH
Built-in variable | Value |
---|---|
STDIN | Standard Input |
STDOUT | Standard Output |
STDERR | Standard Error |
ENV | Environment value |
ARGF | Argument file |
ARGV | Argument value |
DATA | File object after __END__ |
Command Line Option
Option | Operation |
---|---|
-h | Show help |
-v | Show version |
-c | Check ruby grammer, not run |
-e | Run program which are defined as string |
-w | Show warning |
-W0, -W1, -W2, -W | Show warning(according to level) |
-l | Add $LOAD_PATH |
-r | Run file before starting ruby script |
-d | Debug mode |
Comment
# is line comment
# Comment
==begin, ==end is block comment
=begin This is comment =end
RDoc
RDoc is documentation style comment like JavaDoc
RDoc makes HTML style document.
Example
# = Homuhomu class # Homuhomu is nickname of Akemi Homura # class Homuhomu # == madoka Method for test # return value:: 1 def madoka 1 end end