Skip to content

Instantly share code, notes, and snippets.

@shin1ohno
Created March 16, 2013 03:36
Show Gist options
  • Save shin1ohno/5174832 to your computer and use it in GitHub Desktop.
Save shin1ohno/5174832 to your computer and use it in GitHub Desktop.
ruby tutorial

first ruby tutrial

day 1: set up environment

  • install XCode: search 'xcode' in app store and install
  • install 'command line tools': after XCode installed in XCode, install from it's preference pane
  • install home brew: open up terminal, and type ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
  • install rbenv: open up terminal, and type brew install rbenv
  • as said, echo 'eval "$(rbenv init -)"' >> .bashrc', then close terminal window and open again
  • install ruby-build: in the terminal, and type brew install ruby-build
  • install ruby: in the terminal, and type rbenv install 2.0.0-p0

now you have latest ruby installed ! make it default.

rbenv global 2.0.0-p0

day 2: first ruby code

open up terminal and type irb

type puts 'hello' and you get hello

this is ruby code. puts is 'method' and 'hello' is 'argument'. in ruby world, all you have is these 2 things.

ok, let's get deeper.

build the world

open up terminal and type below. don't worry, you don't have to understand this procedure.

cd ~/Desktop
mkdir sandbox
cd sandbox
rbenv local 2.0.0-p0
touch .gitignore Gemfile
echo "source 'https://rubygems.org'" >> Gemfile
echo "gem 'tork', github: 'sunaku/tork'" >> Gemfile
echo "gem 'rspec', github: 'rspec/rspec'" >> Gemfile
echo "gem 'rb-fsevent', '~> 0.9'" >> Gemfile
echo "/tmp" >> .gitignore
echo "/.bundle" >> .gitignore
bundle install --path tmp/bundler
mkdir spec lib
bundle exec rspec --init
bundle exec tork

now you have your own world on the desktop of your mac

bless the man

in spec/man_spec.rb file

require 'rspec'

and save, then tork says

TEST spec/man_spec.rb
PASS spec/man_spec.rb 0

ok, you did right. spec/man_spec.rb is a file to test the world. 'rspec' is a tool to test it.

in spec/man_spec.rb file

require 'rspec'

describe Man do
end

save, tork says

TEST spec/man_spec.rb
FAIL spec/man_spec.rb 256
spec/man_spec.rb:3:in `<top (required)>': uninitialized constant Man (NameError)

of cource, we don't have 'man' definition in our world. let's make it.

open lib/man.rb and

class Man
end

require this from man_spec.rb

require 'rspec'
require_relative '../lib/man'

describe Man do
end

and save, tork says ok. now you have first ruby 'class'.

name the man

now, name the man. in spec/lib/man_spec.rb

require 'rspec'
require_relative '../lib/man'

describe Man do
  it 'is initialized with name' do
    man = Man.new('shin1ohno')
    man.name.should == 'shin1ohno'
  end
end

don't forget the do ! I always do...

save, then

TEST spec/man_spec.rb
FAIL spec/man_spec.rb 256
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
F

Failures:

  1) Man is initialized with name
     Failure/Error: Unable to find matching line from backtrace
     ArgumentError:
       wrong number of arguments (1 for 0)
     # spec/man_spec.rb:6:in `initialize'
     # spec/man_spec.rb:6:in `new'
     # spec/man_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00145 seconds
1 example, 1 failure

oh, we must have implementation for name the man.

in lib/man.rb

class Man
  def initialize(name)
    @name = name
  end
end

save, then

FAIL spec/man_spec.rb 256
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
F

Failures:

  1) Man is initialized with name
     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `name' for #<Man:0x007fd20247bd10 @name="shin1ohno">
     # spec/man_spec.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
1 example, 1 failure

looks like we can named the man, but we can't access the name.

ruby has handy method to solve this : 'attr_accessor'. add this to lib/man.rb

class Man
  attr_accessor :name

  def initialize(name)
    @name = name
  end
end

save

TEST spec/man_spec.rb
PASS spec/man_spec.rb 0

passed

@shin1ohno
Copy link
Author

セットアップ、既存の環境ができあがってるところから始めちゃったので、ちょっと自信がない。もうちょっと要った気がする。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment