Last active
August 29, 2015 14:19
-
-
Save megane42/404ae7ef7eafdef3081f to your computer and use it in GitHub Desktop.
Ruby におけるハッシュ (Hash) と構造体 (Struct) の使い分け ref: http://qiita.com/megane42/items/5bfdb0764fa575efbdab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ハッシュ | |
person = {name: "Taro", age: 16} | |
p person[:name] | |
# 構造体 | |
Person = Struct.new(:name, :age) | |
person = Person.new("Taro", 16) | |
p person.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ハッシュ | |
person = {name: "Taro", age: 16} | |
person[:tel] # => nil | |
person[:sex] = "man" # 要素が追加される | |
# 構造体 | |
Person = Struct.new(:name, :age) | |
person = Person.new("Taro", 16) | |
person.tel # => NameError: undefined method 'tel' | |
person.sex = "man" # => NameError: undefined method 'sex=' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
key = ARGV[0] | |
# ハッシュ | |
p some_hash[key] # => なんとかなる | |
# 構造体 | |
p some_struct.key # => ムリ | |
p some_struct.send(key) # => なんとかなるけど気持ち悪い |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person = Struct.new(:name, :age) | |
person = Person.new("Taro", 16) | |
Robot = Struct.new(:name, :age) | |
robot = Robot.new("Taro", 16) | |
p person == robot # => false. 越えられないカベ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ハッシュ | |
person.to_a | |
=> [[:name, "Taro"], [:age, 16]] | |
# 構造体 | |
person.to_a | |
=> ["taro", 16] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ハッシュ | |
person1 = {name: "Taro", age: 16, most_favorite_movie_ever_seen: "Toy Story"} | |
person2 = {name: "Jiro", age: 17, most_favorite_movie_ever_seen: "Toy Story 2"} | |
person3 = {name: "Maro", age: 18, most_favorite_movie_ever_seen: "Toy Story 3"} | |
# 構造体 | |
Person = Struct.new(:name, :age, :most_favorite_movie_ever_seen) | |
person1 = Person.new("Taro", 16, "Toy Story") | |
person2 = Person.new("Jiro", 17, "Toy Story 2") | |
person3 = Person.new("Maro", 18, "Toy Story 3") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open-uri" | |
class Hoge | |
def call_api | |
response = open "http://something-api/" | |
response.read if response.status[0] == "200" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it '接続成功時は "Success" を返す' do | |
hoge = Hoge.new | |
# read と status に応答できるオブジェクトを即席で作る | |
dummy = Struct.new(:read, :status).new('{"result": "Success"}', ["200", "OK"]) | |
# dummy を返すように open メソッドをスタブ化 | |
allow(hoge).to receive(:open).and_return(dummy) | |
expect(hoge.call_api).to eq '{"result": "Success"}' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment