はじめに
Railsチュートリアルの演習を進めつつ、わからなかったところ、調べたところをアウトプットしてしこうと思います。
4.1
- メディアタイプ: パソコンのスクリーンだけでなく、プロジェクタなどの他の媒体のこと
4.2
4.2.1
演習
>> city = "那覇市"
=> "那覇市"
>> prefecture = "沖縄県"
=> "沖縄県"
>> puts "#{prefecture} #{city}"
沖縄県 那覇市
=> nil
>> puts "#{prefecture}\ #{city}"
沖縄県 那覇市
=> nil
>> puts '#{prefecture}\ #{city}'
=> nil
4.2.2
演習
>> "racecar".length
=> 7
>> "racecar".reverse
=> "racecar"
>> s = "racecar"
=> "racecar"
>> s == s.reverse
=> true
>> puts "It is a palindrome!" if s == s.reverse
It is a palindrome!
=> nil
>> s = "onomatopoeia"
=> "onomatopoeia"
>> puts "It is a palindrome!" if s == s.reverse
=> nil
4.2.3
演習
>> def palindrome_tester(s)
?> if s == s.reverse
?> puts "It's a palindrome!"
?> else
?> puts "It's not a palindrome."
?> end
?> end
=> :palindrome_tester
>> palindrome_tester("racecar")
It's a palindrome!
=> nil
>> palindrome_tester("onomatopoeia")
It's not a palindrome.
=> nil
>> palindrome_tester("racecar").nil?
It's a palindrome!
=> true
4.2.4
def full_title(page_title = '')
end
ここのpage_titleはオプション引数ではなく、デフォルト引数だと思っていたのですが、どうなのでしょうか、、🤔
ちなみにオプション引数は
def full_title(**args)
end
のような形で、任意のキー、バリューを引数に取れるものかと思っています.
このあたり整理してみたいですね 💪
4.3
4.3.1
演習
>> a = "A man, a plan, a canal, Panama".split(", ")
=> ["A man", "a plan", "a canal", "Panama"]
>> s = a.join
=> "A mana plana canalPanama"
>> s = s.split(" ").join
=> "AmanaplanacanalPanama"
>> palindrome_tester(s)
It's not a palindrome.
=> nil
>> palindrome_tester(s.downcase)
It's a palindrome!
=> nil
>> ("a".."z").to_a[6]
=> "g"
>> ("a".."z").to_a[-7]
=> "t"
4.3.2
演習
>> (0..16).map{|i| i**2}
=> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256]
>> def yeller(array)
?> array.map(&:upcase).join
?> end
=> :yeller
>> yeller(['o', 'l', 'd'])
=> "OLD"
>> def random_subdomain
?> ('a'..'z').to_a.shuffle[0..7].join
?> end
=> :random_subdomain
>> random_subdomain
=> "rvuxqobh"
>> def string_shuffle(s)
?> s.split('').shuffle.join
?> end
=> :string_shuffle
>> string_shuffle("foobar")
=> "frooab"
4.3.3
演習
>> hash = {'one' => 'uno', 'two' => 'dos', 'three' => 'tres'}
=> {"one"=>"uno", "two"=>"dos", "three"=>"tres"}
>> hash['one']
=> "uno"
>> hash.each {|key, value| puts "#{key}はスペイン語で#{value}"}
oneはスペイン語でuno
twoはスペイン語でdos
threeはスペイン語でtres
=> {"one"=>"uno", "two"=>"dos", "three"=>"tres"}
>> person1 = {first: '太郎', last: '山田'}
=> {:first=>"太郎", :last=>"山田"}
>> person2 = {first: '二郎', last: '山田'}
=> {:first=>"二郎", :last=>"山田"}
>> person3 = {first: '三郎', last: '山田'}
=> {:first=>"三郎", :last=>"山田"}
>> params = {}
=> {}
>> params[:father] = person1
=> {:first=>"太郎", :last=>"山田"}
>> params[:mother] = person1
=> {:first=>"太郎", :last=>"山田"}
>> params[:mother] = person2
=> {:first=>"二郎", :last=>"山田"}
>> params[:child] = person3
=> {:first=>"三郎", :last=>"山田"}
>> params[:father][:first] == person1[:first]
=> true
>> params[:mother][:first] == person2[:first]
=> true
>> params[:child][:first] == person3[:first]
=> true
>> password_digest = ('a'..'z').to_a.shuffle[0..15].join
=> "cfhogwqsbyuaitrk"
>> user = {name: '沖縄太郎', email: 'test@example.com', password_digest: password_digest}
=> {:name=>"沖縄太郎", :email=>"test@example.com", :password_digest=>"cfhogwqsbyuaitrk"}
>> { "a" => 100, "b" => 200 }.merge({ "b" => 300 })
=> {"a"=>100, "b"=>300}
4.4.1
- コンストラクタ: RubyのリファレンスでInitializeについて見てみると以下のような記述がありました.
このメソッドは Class#new から新しく生成されたオブジェクトの初期化のために呼び出されます。他の言語のコンストラクタに相当します。デフォルトの動作ではなにもしません。
つまり、RubyでいうInitializeが他の言語のコンストラクタにあたる、ということでしょうか、、 🤔
s = "foobar"
の ""
は暗黙的に String.new
をおこなっているという認識です.
演習
>> (1..10)
=> 1..10
>> Range.new(1, 10)
=> 1..10
>> (1..10) == Range.new(1, 10)
=> true
4.4.2
演習
>> Hash.superclass
=> Object
>> Hash.superclass.superclass
=> BasicObject
>> Symbol.superclass
=> Object
>> Symbol.superclass.superclass
=> BasicObject
>> class Word < String
?> def palindrome?
?> self == reverse
?> end
?> end
=> :palindrome?
>> s = Word.new('level')
=> "level"
>> s.palindrome?
=> true
4.4.3
演習
>> s = Word.new("racecar")
=> "racecar"
>> s.palindrome?
=> true
>> s = Word.new("onomatopeia")
=> "onomatopeia"
>> s.palindrome?
=> false
>> s = Word.new("Malayalam")
=> "Malayalam"
>> s.downcase.palindrome?
=> true
>> class String
?> def shuffle
?> self.split('').shuffle.join
?> end
?> end
=> :shuffle
>> "foobar".shuffle
=> "bfraoo"
>> class String
?> def shuffle
?> split('').shuffle.join
?> end
?> end
=> :shuffle
>> "foobar".shuffle
=> "orofba"
4.4.4
演習
toyアプリケーションを作ってないので本演習は飛ばします
4.4.5
attr_accessor :name
とすることでname の呼び出し、更新ができるようになります.
演習
class User
attr_accessor :first_name, :last_name, :email
def initialize(attributes = {})
@first_name = attributes[:first_name]
@last_name = attributes[:last_name]
@email = attributes[:email]
end
def formatted_email
"#{full_name} <#{@email}>"
end
def full_name
"#{@first_name} #{@last_name}"
end
def alphabetical_name
"#{@last_name}, #{@first_name}"
end
end
>> require './example_user'
=> true
>> user = User.new(first_name: 'Michael', last_name: 'Hartl', email: 'test@example.com')
=>
>> user.formatted_email
=> "Michael Hartl <test@example.com>"
>> user.alphabetical_name
=> "Hartl, Michael"
>> user.full_name.split == user.alphabetical_name.split(', ').reverse
=> true
まとめ
オブジェクト指向の考え方、Classの使い方がしっかり落とし込めました💪
解答に間違いがありましたらお手数ですがコメントいただけると幸いです 🙇