七周七 -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阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容

  • UI设计图都是带圆角的,简单写一个 Shape 属性搞定。但是需要每个 Shape 属性的背景颜色都不一样,那就需...
    simpleeeeee阅读 17,638评论 2 11
  • 小学的时候,我和姐姐跟着爷爷奶奶生活。小时候,我家的门前有一株葡萄藤,葡萄藤的前面有一个晒稻谷用的场地,场地的前面...
    廉隅阅读 681评论 0 2
  • 婆划三分客,殊途六月寒。 娇娘多宠爱,九载满辛酸。
    秋风起花香阅读 175评论 2 4
  • 记得小时候 天是纯粹的蓝 云是大朵的白 有阳光穿过 似有金线镶着边 学校要我们全体到操场考试 太阳晒得人懒洋洋 我...
    布之兮阅读 207评论 0 1