# 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#轉換為整數(shù)類型
puts '100.0'.to_i
puts 'hello'.to_i
puts 100.to_f#轉換為float類型的
# 3.times{puts 'hello'}#能夠執(zhí)行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
#關于數(shù)組
animals = ['lions','tigers','dogs']
puts animals.count#在這里使用.length 與.count都能返回數(shù)組的個數(shù)
puts animals[-1]#返回數(shù)組中的倒數(shù)第一個元素
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#暫停,去執(zhí)行塊里的代碼
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}
七周七 -ruby 1,2
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來兜蠕,“玉大人扰肌,你說我怎么就攤上這事⌒苎睿” “怎么了曙旭?”我有些...
- 正文 為了忘掉前任剂习,我火速辦了婚禮,結果婚禮上较沪,老公的妹妹穿的比我還像新娘鳞绕。我一直安慰自己,他們只是感情好尸曼,可當我...
- 文/花漫 我一把揭開白布猾昆。 她就那樣靜靜地躺著,像睡著了一般骡苞。 火紅的嫁衣襯著肌膚如雪垂蜗。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼档悠,長吁一口氣:“原來是場噩夢啊……” “哼廊鸥!你這毒婦竟也來了?” 一聲冷哼從身側響起辖所,我...
- 正文 年R本政府宣布,位于F島的核電站擎宝,受9級特大地震影響郁妈,放射性物質發(fā)生泄漏。R本人自食惡果不足惜绍申,卻給世界環(huán)境...
- 文/蒙蒙 一噩咪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧极阅,春花似錦胃碾、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至奔脐,卻和暖如春俄周,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背髓迎。 一陣腳步聲響...
推薦閱讀更多精彩內容
- UI設計圖都是帶圓角的拴曲,簡單寫一個 Shape 屬性搞定。但是需要每個 Shape 屬性的背景顏色都不一樣凛忿,那就需...