# 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}
七周七 -ruby 1,2
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- UI设计图都是带圆角的,简单写一个 Shape 属性搞定。但是需要每个 Shape 属性的背景颜色都不一样,那就需...