Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Ruby for Starter
高見龍
March 23, 2013
Programming
3
400
Ruby for Starter
高見龍
March 23, 2013
Tweet
Share
More Decks by 高見龍
See All by 高見龍
閱讀原始碼 - 再戰十年的 jQuery
eddie
1
460
Learn JavaScript Well
eddie
1
1.1k
How to Learn Web Framework Correctly
eddie
4
1.8k
about-5xruby
eddie
0
90
Ruby on Rails 2018 年進化論
eddie
0
120
10 years in learning Ruby
eddie
5
1.3k
傳說中可以治百病的區塊鏈是怎麼一回事?
eddie
0
1.7k
Git Branch
eddie
0
320
Refactoring (Ruby edition)
eddie
0
220
Other Decks in Programming
See All in Programming
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
42k
LIFFで動く割り勘アプリTATEKAをリリースしてみた話
inoue2002
0
170
低レイヤーから始める GUI
fadis
18
9.2k
ECS Service Connectでマイクロサービスを繋いでみた
xblood
0
510
Amebaブログの会員画面システム刷新の道程
ryotasugawara
1
210
スタック・オーバーフローに コントリビュートしはじめて良かったこと🐣
takuyakikuchi
1
120
OSSから学んだPR Descriptionの書き方
fugakkbn
4
120
23年のJavaトレンドは?Quarkusで理解するコンテナネイティブJava
tatsuya1bm
1
110
CDKでValidationする本当の方法 / cdk-validation
gotok365
1
150
Istio⛵️によるサービスディスカバリーの仕組み
hiroki_hasegawa
3
1.3k
【DevFest & ADS JP 22】チームで導入する
[email protected]
おいしい健康
kako351
0
210
OIDC仕様に準拠した Makuake ID連携基盤構築の裏側
ymtdzzz
0
130
Featured
See All Featured
How GitHub (no longer) Works
holman
298
140k
Build your cross-platform service in a week with App Engine
jlugia
221
17k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
2
390
YesSQL, Process and Tooling at Scale
rocio
159
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
29
7.7k
For a Future-Friendly Web
brad_frost
166
7.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
16k
GraphQLの誤解/rethinking-graphql
sonatard
39
7.8k
Writing Fast Ruby
sferik
613
58k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
15
1.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
346
17k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
152
13k
Transcript
Ruby Starter
[email protected]
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