七周七 -ruby 1,2

# puts "hello"
#
# language = "mudy"
#
# puts "hello,#{language}"

# 4
# puts 4.class
# puts 4.methods


x = 4
puts 'This appears to be false' unless x == 4 #除非x=3才會打印這句話,

puts x <5

puts false.class

if x ==4 
  puts 'This appears to be ture'
end

unless x == 4
  puts 'This appers to be false'
else
  puts 'This appears to be ture'
end

# x = x-1  until x==0 

puts 'This is ture' if true
puts '中文' unless false
puts '不滿足條件才會進來' unless x==3

puts '滿足條件才會進來' if true

puts nil or true#臥槽,這個的值你猜是啥
puts true or nil

puts nil and true
puts "-------------"
puts true and false
puts false and true
puts "-------------"
# puts ture and this_will_cause_an_error
puts false && this_will_cause_an_error

# puts true | this_will

# puts 4+'four'
puts "-------------"
puts 'four'.class
puts 4.class
puts 4.0.class
puts true.class
puts false.class

puts "-------------"

def add_them_up
  4 + 4.0
end

puts add_them_up
puts "-------------"

puts 100.0.to_i#轉換為整數類型
puts '100.0'.to_i
puts 'hello'.to_i
puts 100.to_f#轉換為float類型的


# 3.times{puts 'hello'}#能夠執行3次{}中的部分
range1 = (1..10).to_a#將range轉換成arr類型
puts range1
puts "#{range1}"

puts "-------------"

range2 = ('baa'..'ddz').to_a
# puts "#{range2}"#這一句特別逗

digits = 0..9
puts digits.include?(5)

puts digits.min
puts digits.max
puts "--------------"
# "Hello Ruby.".scan(/[Ruby\.]/){|x|puts "Hello Ruby.".index(x)}
# "Hello Ruby.".scan(/[Ruby\.]/){|x|puts x}
puts "Hello Ruby".index('Ruby')#在"Hello, Ruby."中,出"Ruby."所在下標。

puts "-----------第二天-------------"


def tell_the_truth
  true
end

print tell_the_truth#print打印是不換行的

#散列表
numbers = {1=>'one',2=>'two'}
print numbers 

puts numbers[1]
puts 'string'.object_id
puts 'string'.object_id
puts 'string'.object_id
puts :string.object_id#:symbol符號,就是一種前面帶有冒號的標識符
puts :string.object_id

def tell_the_truth(options={})
  if options[:profession] == :lawyer
    'it could be believed that this is almost certainly not false'
  else
    true
  end
end

puts tell_the_truth
puts tell_the_truth ({:profession=>:lawyer})#({})可以省略

x = 2
# print x.methods
# [:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :quo, :remainder, :real?, :nonzero?, :step, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

puts x.next
puts x.+@
puts x.to_enum

#關于數組
animals = ['lions','tigers','dogs']
puts animals.count#在這里使用.length 與.count都能返回數組的個數

puts animals[-1]#返回數組中的倒數第一個元素
puts animals[4]#越界訪問并沒有報錯
puts animals.class
# print animals.methods
# [:inspect, :to_s, :to_a, :to_ary, :frozen?, :==, :eql?, :hash, :[], :[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift, :unshift, :insert, :each, :each_index, :reverse_each, :length, :size, :empty?, :find_index, :index, :rindex, :join, :reverse, :reverse!, :rotate, :rotate!, :sort, :sort!, :sort_by!, :collect, :collect!, :map, :map!, :select, :select!, :keep_if, :values_at, :delete, :delete_at, :delete_if, :reject, :reject!, :zip, :transpose, :replace, :clear, :fill, :include?, :<=>, :slice, :slice!, :assoc, :rassoc, :+, :*, :-, :&, :|, :uniq, :uniq!, :compact, :compact!, :flatten, :flatten!, :count, :shuffle!, :shuffle, :sample, :cycle, :permutation, :combination, :repeated_permutation, :repeated_combination, :product, :take, :take_while, :drop, :drop_while, :bsearch, :pack, :entries, :sort_by, :grep, :find, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :any?, :one?, :none?, :min, :max, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :lazy, :nil?, :===, :=~, :!~, :class, :singleton_class, :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

class Fixnum
  def my_times
    i = self
    while i > 0
      i = i - 1
      yield#暫停,去執行塊里的代碼
    end
  end
end
4.my_times{puts 'frfr'}


class Tree
  attr_accessor :children,:node_name#??,這個實例變量名字寫錯了,說我未定義
  def initialize(name, children=[])
    @children = children
    @node_name = name
  end
  
  def visit_all(&block)
    visit &block
    children.each{|c| c.visit_all &block}
  end
  
  def visit(&block)
    block.call self
  end
end

ruby_tree = Tree.new("Ruby",[Tree.new("Reia"),Tree.new("Bob"),Tree.new("lucy")])
puts "visiting a tree" 
ruby_tree.visit{|node| puts node.node_name}
puts "visiting entire tree" 
ruby_tree.visit_all{|node|puts node.node_name}

puts 'begin'<=> 'end'
a = [1,4,3,6,2,8,5]
print a.sort
print a.inject(){|sum,i| sum+i}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,565評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,319評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內容

  • UI設計圖都是帶圓角的,簡單寫一個 Shape 屬性搞定。但是需要每個 Shape 屬性的背景顏色都不一樣,那就需...
    simpleeeeee閱讀 17,725評論 2 11
  • 小學的時候,我和姐姐跟著爺爺奶奶生活。小時候,我家的門前有一株葡萄藤,葡萄藤的前面有一個曬稻谷用的場地,場地的前面...
    廉隅閱讀 721評論 0 2
  • 婆劃三分客,殊途六月寒。 嬌娘多寵愛,九載滿辛酸。
    秋風起花香閱讀 201評論 2 4
  • 記得小時候 天是純粹的藍 云是大朵的白 有陽光穿過 似有金線鑲著邊 學校要我們全體到操場考試 太陽曬得人懶洋洋 我...
    布之兮閱讀 219評論 0 1