z, ? | toggle help (this) |
space, → | next slide |
shift-space, ← | previous slide |
d | toggle debug mode |
## <ret> | go to slide # |
c, t | table of contents (vi) |
f | toggle footer |
r | reload slides |
n | toggle notes |
p | run preshow |
:about_me => {
:name => "Samnang Chhun",
:work_at => ["Yoolk Inc.", "Freelancer"],
:website => "http://wowkhmer.com",
:email => "[email protected]",
:twitter => "@samnangchhun",
:certificattions => ["MCTS"],
:communities => [
"Ruby Mendicant University",
"RubyLearning",
"ShareVisionTeam",
"Pailin"
]
}
let students test with code snippets on http://tryruby.org
"I love Ruby" * 5
5.times { puts "I love Ruby" }
[1, 3, "samnang"].last.capitalize
$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
$ irb
ruby-1.9.2-p180 :001 > puts "I love Ruby!"
I love Ruby!
=> nil
let students test codes in irb
"I love Ruby" * 5
5.times { puts "I love Ruby" }
[1, 3, "samnang"].last.capitalize
5.times { print "Ruby! "}
print "Ruby! " * 5
100.next
'a'.next
1.upto(5) {|x| puts x }
5.downto(1) { |x| puts x }
100.next #=> 101
'a'.next #=> "b"
100.class #=> Fixnum
"Ruby".class #=> String
[].class #=> Array
{}.class #=> Hash
5.times { print "Ruby! " }
#=> Ruby! Ruby! Ruby! Ruby! Ruby!
5 + 5
5.+(5) # both are the same
numbers = ['zero', 'one', 'two', 3]
numbers[0] #=> 'zero'
numbers.first #=> 'zero'
numbers.last #=> 3
numbers[-1] #=> 3
list = []
list << 'first'
list << 'bar'
list.pop # can be used as a stack
profile = {
:name => "Samnang",
:age => 100 #Who knows :-)
}
profile[:name]
profile[:age]
range = 1..5
range.to_a #=> [1, 2, 3, 4, 5]
range = 'a'..'z'
range.to_a # array of strings from a to z
range = 1...5
range.to_a #=> [1, 2, 3, 4]
range.include? 2 #=> true
array = [1, 2, 3, 4]
array[0..2] #=> [1, 2, 3]
array[1...-1] #=> [2, 3]
if something_true?
do_something
else
do_another_thing
end
puts "I'll love Ruby" if not absence_today?
puts "I'll love Ruby" unless absence_today?
%w{Ruby HTML5 CSS3}.each do |lang|
puts "#{lang} is so cool!"
end
see some classes on both ruby core and standard library
(String, Fixnum, Array, ...)
(CSV, ERB, Webrick, ...)
class BankAccount
attr_accessor :name, :account_no
attr_reader :balance
def initialize(name)
@name = name
@balance = 0
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance += amount
end
def active?
#is account active or not
end
end
account = BankAccount.new("Samnang")
class Animal
# ...
end
class Cat < Animal
# ....
end
class Dog < Animal
# ...
end
module A
def say_hello
puts "Hello"
end
end
module B
def say_goodbye
puts "Goodbye"
end
end
class C
include A
include B
end
obj = C.new
obj.say_hello
obj.say_goodbye
module XML
class Parser
# ...
end
end
module PDF
class Parser
# ...
end
end
XML::Parser
PDF::Parser
animal = "cat"
another_aminal = "dog"
def animal.speak
puts "The #{self} says miaow"
end
animal.speak # The cat says miaow
another_animal.speak
# NoMethodError: undefined method `speak' for "dog":String
demo how to access singleton class in Ruby 1.8 and Ruby 1.9
(Eigenclasses/Metaclasses/Virtual Classes/Ghost classes)
class Duck
def quack
puts "Quack!"
end
end
class DuckRecording
def quack
play
end
def play
'Quack!'
end
end
[Duck.new, DuckRecording.new].each { |a| a.quack }
class Array
def second
self[1]
end
end
[1, 2, 3].second # => 2
require 'active_support/core_ext'
2.days.from_now # => 2011-06-20 14:14:11 +0700
"person".pluralize # => "people"
class Person
attr_accessor :name, :age
end
class Person
def name
@name
end
def name=(value)
@name = value
end
def age
@age
end
def age=(value)
@age = value
end
end
class Person
%w(name age).each do |method_name|
define_method(method_name) do
instance_variable_get("@#{method_name}")
end
define_method("#{method_name}=") do |value|
instance_variable_set("@#{method_name}", value)
end
end
end
class Person
attr_accessor :name, :age
end
demo some codes