開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『Learning Ruby』(Michael Fitzgerald 著、O'Reilly Media、2007年、ISBN978-0-596-52986-4)の Chapter 7(Hash)Review Questions 6を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
6.
Nothing
コード(TextWrangler)
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
h = {'a' => 1,'b' => 2}
['a','b','c',1,2].each do |s|
puts "#{h.has_key? s} #{h.key? s}"
end
入出力結果(Terminal)
$ ri Hash.has_key? >> tmp.txt
$ cat tmp.txt
---------------------------------------------------------- Hash#has_key?
hsh.has_key?(key) => true or false
hsh.include?(key) => true or false
hsh.key?(key) => true or false
hsh.member?(key) => true or false
------------------------------------------------------------------------
Returns +true+ if the given key is present in _hsh_.
h = { "a" => 100, "b" => 200 }
h.has_key?("a") #=> true
h.has_key?("z") #=> false
$ ./sample.rb
true true
true true
false false
false false
false false
$
0 コメント:
コメントを投稿