Ruby

Blocks

iterator

puts "express gratitude tree times" 3.times{puts "thank you!"} #Express gratitude three times data=1,2,3 puts "print each element x of data=1,2,3" data.each{|x| puts x} #print each element x of data puts "compute squares of array elements[…

loop expression

puts "how_many_message" def how_many_messages(n) "You have "+n.to_s+(n==1 ? "message." : "messages.") end puts "while loop\n" x=10 while x >=0 do puts x x=x-1 endputs "until loop\n" x=0 until x>10 do puts x x=x+1 endputs "while modifier" x…

Control Structures

x=ARGV[0].to_f #Convert first argument to a number y=ARGV[1].to_f #convert second argument to a number sum=x+y #Add the arguments puts sum #Print the sum#if x is less than 10, increment it #same simple conditional x=1 while x if x x+=1 put…

Expressions and Operator

#method invocations puts "hello world" #"puts" invoked on self , with one string arg puts Math.sqrt(2) #"sqrt" invoked on object Math with one arg message="hello world" puts message.length #"length" invoked on object message;no args a=[0,1…

Programming Ruby Object

#Object #string t copyed from string s s="Ruby" t=s p t #=>"Ruby" s="Rails" p t #=>"Ruby" p s #=>"Rails" t[-1]="" print s #=>Rails puts "\n" print t #=>Rubo="test" o.class o.class.superclass o.class.superclass.superclass if o.class==String…

Programming Ruby Range

#Range cold_war=1946..1986 birthday_year=1974 if cold_war.include? birthday_year puts "include birthday_year" else puts "not include birthday_year" enda="a".."c" a.each{|l| print "#{l}"} #abc puts "\n" a.step(2){|l| print "#{l}"} #=>ac put…

Programming Ruby Hashes

#hashes numbers=Hash.new numbers["one"]=1 numbers["two"]=2 numbers["three"]=3sum=numbers["one"]+numbers["two"]+numbers["three"] puts summembers={"one"=>1,"two"=>2,"three"=>3} p members members={:one=>1,:two=>2,:three=>3} p membersreturn {"…

Ruby on Rails install

Railsをインストールする前に gem update しておきましょう。 $ gem update rails のインストール $ sudo gem install rails$ rbenv rehashRails がインストールされたかどうか確認してみましょう。$ rails -v Rails 3.2.9、試しにやってみましょう。$ rails…

Programming Ruby Array

#arraywords=%w|({ puts words white=%W(s\s s\t s\r s\n) #same as:["s\s","s\t","s\r","s\n"] puts whiteempty=Array.new #:returns a new empty array nils=Array.new(3) #[nil,nil,nil]:new array with 3 nil elements zeros=Array.new(4,0) #[0,0,0,0]:…

Programming Ruby multibyte String

s="2×2=4" p s.bytesize #=>6 s.bytesize.times{|i| print s.getbyte(i)," "} #=> 50,195,151,50,61,52 puts "\n" p s.length #=>5s.length.times{|i| print s[i], " "} #=>2 × 2 = 4s.setbyte(5,s.getbyte(5)+1) #s is now "2×2=5" s.length.times{|i| prin…

Programming Ruby String

String literal "string" 'string'?\u20AC==?€ concatenate string and >= s='hello' puts s[0] #output 'h' puts s[s.length-1] #output 'o' puts s[-2] #output 'l' puts s[-s.length] #output 'h' puts s[s.length] #nil :there is no character at that …

Programming Ruby

[here document] greeting=> Hello There World

Ruby学習 loop

;while loop you don't know how many times you'll be looping. ;until loop 1 between 10 counter=1 until counter==11 puts counter counter+=1 end;for loop you know how many times you'll be looping. for num in 1..10 puts num end;loop method i=2…

Ruby学習 condition

Rubyの文法は理解しやすいので勉強時間が少なくて済みそうです。if文の書き方if #条件 #動作 elsif #条件 #動作 else #動作 endincludeメッソッドを使用して stringに文字列が含まれているか検索して 一致する場合は文字列を置き換えるuser_input=gets.chomp…