Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby for Starter
Search
高見龍
March 23, 2013
Programming
3
420
Ruby for Starter
高見龍
March 23, 2013
Tweet
Share
More Decks by 高見龍
See All by 高見龍
Generative AI 年會小聚 - AI 教我寫程式
eddie
0
18
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
400
AI 時代的程式語言學習法
eddie
0
59
前端模組解放運動 - importmap
eddie
0
1.2k
Git 和 DevOps - 在混亂的流星群開發流程中找到小確幸
eddie
1
1.1k
模組化前端開發:從亂七八糟到組織有序
eddie
0
1.5k
被 Vue 框架耽誤的建置工具
eddie
2
950
開開心心寫測試,你的程式碼也會微笑
eddie
1
1.2k
Functional Ruby
eddie
1
260
Other Decks in Programming
See All in Programming
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
初めてDefinitelyTypedにPRを出した話
syumai
0
420
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
3
230
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
Remix on Hono on Cloudflare Workers
yusukebe
1
300
最新TCAキャッチアップ
0si43
0
200
Realtime API 入門
riofujimon
0
150
Better Code Design in PHP
afilina
PRO
0
130
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Outline View in SwiftUI
1024jp
1
340
Featured
See All Featured
The Invisible Side of Design
smashingmag
298
50k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Designing for Performance
lara
604
68k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
A designer walks into a library…
pauljervisheath
204
24k
A Philosophy of Restraint
colly
203
16k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Thoughts on Productivity
jonyablonski
67
4.3k
Transcript
Ruby Starter eddie@fju
None
current status
80% iOS app, 20% Ruby/Rails
What I want?
Problem Solving
History
まつもと ゆきひろ (Matz)
first released at 1995
2.0 released at 2013
None
Why Ruby? free, open source, easy to learn
Ruby != Rails
Happy, and Fun
Rubies CRuby(MRI), REE, mRuby, JRuby, IronRuby, Rubinius..etc
Version 1.8, 1.9, 2.0
Ruby 1.8 has no future
RVM Ruby Version Manager https://rvm.io/
Editors Vim, Emacs, TextMate, Sublime Text... etc
git
coding style https://github.com/styleguide/ruby
Variables and Constants
local variable variable
global variable $variable
instance variable @variable
class variable @@variable
virtual variable true, false, self, nil
variable assignment a = 1 x, y, z = 1,
2, 3
Constant begins with a capital letter, and it can be
changed
Reserved word and Keyword
Logic and Flow Control
only false and nil are false
true v.s. TrueClass false v.s. FalseClass nil v.s. NilClass
if..elsif..end
unless = if not
if modifier
case .. when..
BEGIN{} and END{}
a = true ? 'a' : 'b'
a ||= 'a'
Comment # single line
Comment =begin .. =end
Loop and Iteration
for.. in..
while .. end
until .. end
until = while not
times
upto, downto
each, each_with_index
Block
Proc
my_square = Proc.new { | x | x ** 2
} my_square.call(10) # 100 my_square[10] # 100
lambda, ->
my_lambda = lambda { | x | x ** 2
} # new style in 1.9 my_lambda = -> x { x ** 2 } # how to call a lambda? my_lambda.call(10) my_lambda[10]
Proc v.s. lambda
def proc_test puts "hello" my_proc = Proc.new { return 1
} my_proc.call puts "ruby" end def lambda_test puts "hello" my_lambda = lambda { return 1 } my_lambda.call puts "ruby" end
{} v.s. do..end http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/
Number
Fixnum and Bignum
10 / 3
String http://ruby-doc.org/core-1.9.2/String.html
single and double quotes
%q v.s. %Q
"%s" % "eddie"
string interpolation
Array http://ruby-doc.org/core-1.9.2/Array.html
Array.new v.s. []
%w
Hash http://ruby-doc.org/core-1.9.2/Hash.html
Hash.new v.s {}
a = { :name => 'eddie' } a = {
name: 'eddie' }
Range http://ruby-doc.org/core-1.9.2/Range.html
(1..10) v.s. (1...10)
Methods
def method_name(param) ... end
parentheses can be omitted
? and !
return value
Singleton Method
class Cat def walk puts "I'm walking" end end cat
= Cat.new def cat.fly puts "I can fly" end cat.fly
Method Missing
def method_missing(method_name) puts "method: #{method_name} is called!" end [1, 2,
3, 4].hello
Exception Handling begin .. rescue.. else.. ensure.. end
def open_my_file(file_name) File.open file_name do |f| puts f.read end end
begin open_my_file("block_demo.r") rescue => e puts e else puts "it's working good!" ensure puts "this must be executed, no matter what" end
Object-Oriented Programming
everything in Ruby is an Object
object = state+ behavior
top class = Object top class would be BasicObject in
Ruby 1.9
class ClassName < ParentClass ... end
Naming Convention
initialize
ClassName.new
self = current object
instance and class variable
instance and class method
public, protected and private method
getter and setter
attr_reader, attr_writer and attr_accessor
Open Class
Module
module ModuleName ... end
module has no inheritance
module has no instance
Naming Convention
require v.s. load
Mixin
Ruby is single inheritance
Duck Typing
include v.s. extend
Gem
gem install PACKAGE_NAME
gem env
Bundle
Gemfile
gem "nokogiri", :git => "git://github.com/tenderlove/nokogiri.git" gem "secret_gem", :path => "~/my_secret_path"
bundle install
pack your own gem!
1. bundle gem NEW_NAME 2. gem build NEW_NAME.gemspec 3. gem
push NEW_NAME.gem http://docs.rubygems.org/read/chapter/20
Rake
Ruby Object Model