- 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
- Ruby is an open-source and is freely available on the Web, but it is subject to a license.
- Ruby is a general-purpose, interpreted programming language.
- Ruby is a true object-oriented programming language.
- Ruby is a server-side scripting language similar to Python and PERL.
- Ruby can be used to write Common Gateway Interface (CGI) scripts.
- Ruby can be embedded into Hypertext Markup Language (HTML).
- Ruby has a clean and easy syntax that allows a new developer to learn Ruby very quickly and easily.
- Ruby has similar syntax to that of many programming languages such as C++ and Perl.
- Ruby is very much scalable and big programs written in Ruby are easily maintainable.
- Ruby can be used for developing Internet and intranet applications.
- Ruby can be installed in Windows and POSIX environments.
- Ruby support many GUI tools such as Tcl/Tk, GTK, and OpenGL.
- Ruby can easily be connected to DB2, MySQL, Oracle, and Sybase.
- Ruby has a rich set of built-in functions, which can be used directly into Ruby scripts.
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.
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
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'.
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
counter = 1
while counter < 11
puts counter
counter = counter + 1
end
i = 0
until i == 6
i += 1
end
puts i
counter = 1
until counter > 10
puts counter
counter += 1 #Add 1 to counter, then assign that new value back to counter.
end #Assignment operators: (+=), (-=), (*=), and (/=)
#The 'For' Loop for num in 1...10 #For the variable num in the range 1 to 10, do the following: puts num #Three dots(...): to exclude the final number in the count, go up to but don't include 10. end #Two dots(..): to include the highest number in the range.
#The Loop Method: iterator #Curly braces ({}) are generally interchangeable with the keywords do (to open the block) and end (to close it)
i = 20 loop { i -= 1 print "#{i}" break if i <= 0 }
i = 20 loop do i -= 1 print "#{i}" break if i <= 0 end
#Next: be used to skip over certain steps in the loop. #don't want to print out the even numbers for i in 1..5 next if i % 2 == 0 print i end
i = 20 loop do i -= 1 next if i % 2 != 0 print "#{i}" break if i <= 0 end
#array: multiple values into a single variable #An array is just a list of items between square brackets([]) my_array = [1,2,3,4,5]
#The .each Iterator: apply an expression to each element of an object, one at a time. #type 1 array = [1,2,3,4,5]
array.each do |x| x += 10 print "#{x}" end #type 2 odds = [1,3,5,7,9]
odds.each do |x| x *= 2 print "#{x}" end
#The .times Iterator: perform a task on each item in an object a specified number of times. 10.times { print "Chunky bacon!" }
#print 1 to 50, iclusive
i=1 while i<51 do print i i+=1 end
i=1 until i<51 do print i i+=1 end #type 3: for for i in 1..50 print i end
x=0 loop do x+=1 print "Ruby!" break if x==30 end
30.times {print "Ruby!"}