ror突进-p2

irb

貌似是个ruby的交互式命令行
localhost:~ alan$ irb
2.3.1 :001 >
2.3.1 :002 > 1+2
=> 3
2.3.1 :003 > require "active_support"
=> true
2.3.1 :004 > exit

变量

localhost:~ alan$ irb
2.3.1 :001 > a = 1
=> 1
2.3.1 :002 > a + 1
=> 2
2.3.1 :003 > b = 2
=> 2
2.3.1 :004 > a+b
=> 3
2.3.1 :005 > fruits = ["kiwi", "strawberry", "plum"]
=> ["kiwi", "strawberry", "plum"]
2.3.1 :006 > fruits = fruits + ["orange"]
=> ["kiwi", "strawberry", "plum", "orange"]
2.3.1 :007 > fruits = fruits - ["kiwi"]
=> ["strawberry", "plum", "orange"]

试了一些命令发现变量相关语法异常简单好记,看得出这是一个相当现代的编程语言

2.3.1 :009 > 7.class
=> Fixnum
2.3.1 :010 > "kiwi".class
=> String
2.3.1 :011 >
2.3.1 :012 > fruits.class
=> Array
2.3.1 :013 >

变量类型可以使用class来确定,和其他语言基本是一致的,除了『int整形』换成了『Fixnum数值型』

2.3.1 :015 > 1.0.class
=> Float
2.3.1 :016 > 1.calss
NoMethodError: undefined method calss' for 1:Fixnum
Did you mean? class
from (irb):16
from /Users/alan/.rvm/rubies/ruby-2.3.1/bin/irb:11:in

'
2.3.1 :017 > 1.class
=> Fixnum
2.3.1 :018 > 2.calss
NoMethodError: undefined method calss' for 2:Fixnum
Did you mean? class
from (irb):18
from /Users/alan/.rvm/rubies/ruby-2.3.1/bin/irb:11:in
'
2.3.1 :019 > 2.class
=> Fixnum
2.3.1 :020 > b.class
=> Fixnum
2.3.1 :021 > a.class
=> Fixnum
2.3.1 :022 > f.class
NameError: undefined local variable or method f' for main:Object
Did you mean? fg
from (irb):22
from /Users/alan/.rvm/rubies/ruby-2.3.1/bin/irb:11:in
'
2.3.1 :023 > 'f'.class
=> String
2.3.1 :024 > d = {'a':3}
=> {:a=>3}
2.3.1 :025 > d.class
=> Hash
2.3.1 :026 > g=[{'a':2},{'b':3}]
=> [{:a=>2}, {:b=>3}]
2.3.1 :027 > g
=> [{:a=>2}, {:b=>3}]
2.3.1 :028 > g.class
=> Array

额外又尝试了几个其他的类型,多看到了一个float,还有hash,并且可以直接嵌套

2.3.1 :040 > fruits.methods
=> [:fill, :assoc, :rassoc, :uniq, :uniq!, :compact, :compact!, :flatten, :to_h, :flatten!, :shuffle!, :shuffle, :include?, :combination, :repeated_permutation, :permutation, :product, :sample, :repeated_combination, :bsearch_index, :bsearch, :select!, :&, :, :+, :-, :sort, :count, :find_index, :select, :reject, :collect, :map, :pack, :first, :any?, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :insert, :|, :index, :rindex, :replace, :clear, :<=>, :<<, :==, :[], :[]=, :reverse, :empty?, :eql?, :concat, :reverse!, :inspect, :delete, :length, :size, :each, :slice, :slice!, :to_ary, :to_a, :to_s, :dig, :hash, :at, :fetch, :last, :push, :pop, :shift, :unshift, :frozen?, :each_index, :join, :rotate, :rotate!, :sort!, :collect!, :map!, :sort_by!, :keep_if, :values_at, :delete_at, :delete_if, :reject!, :transpose, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :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, :slice_after, :slice_when, :chunk_while, :lazy, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :is_a?, :extend, :define_singleton_method, :to_enum, :enum_for, :===, :=~, :!~, :respond_to?, :freeze, :display, :send, :object_id, :method, :public_method, :singleton_method, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :public_methods, :singleton_methods, :!, :!=, :send, :equal?, :instance_eval, :instance_exec, :id]
2.3.1 :041 > 1.methods
=> [:%, :&, :
, :+, :-, :/, :<, :>, :^, :|, :~, :-@, :**, :<=>, :<<, :>>, :<=, :>=, :==, :===, :[], :inspect, :size, :succ, :to_s, :to_f, :div, :divmod, :fdiv, :modulo, :abs, :magnitude, :zero?, :odd?, :even?, :bit_length, :to_int, :to_i, :next, :upto, :chr, :ord, :integer?, :floor, :ceil, :round, :truncate, :downto, :times, :pred, :to_r, :numerator, :denominator, :rationalize, :gcd, :lcm, :gcdlcm, :+@, :eql?, :singleton_method_added, :coerce, :i, :remainder, :real?, :nonzero?, :step, :positive?, :negative?, :quo, :arg, :rectangular, :rect, :polar, :real, :imaginary, :imag, :abs2, :angle, :phase, :conjugate, :conj, :to_c, :between?, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :is_a?, :extend, :define_singleton_method, :to_enum, :enum_for, :=~, :!~, :respond_to?, :freeze, :display, :send, :object_id, :method, :public_method, :singleton_method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :send, :equal?, :instance_eval, :instance_exec, :id]

尝试了下method,可以直接把变量的所有方法show出来,这个出来的结果本身也是个Array

2.3.1 :046 > fruits.each do |fruit|
2.3.1 :047 > puts fruit
2.3.1 :048?> end
strawberry
plum
orange
=> ["strawberry", "plum", "orange"]

看得出ruby的遍历十分简洁,直接使用each就能实现原来循环才能做的操作
2.3.1 :053 > fruits.each do |b|
2.3.1 :054 > puts b
2.3.1 :055?> end

稍微调整试了下发现原来这个fruit指的就是遍历出来的值
而在ruby里面的打印使用的是『puts』

逻辑语句(分支)

2.3.1 :059 > my_variable = 2
=> 2
2.3.1 :060 > if my_variable > 1
2.3.1 :061?> puts "YAY!"
2.3.1 :062?> end
YAY!
=> nil

这个NIL还不知道是啥

函数

2.3.1 :068 > def pluralize(word)
2.3.1 :069?> word + "s"
2.3.1 :070?> end
=> :pluralize
2.3.1 :071 > pluralize("kiwi")
=> "kiwis"

语法确实很简洁实在是多一分显胖少一分显瘦,不过目前看来返回值只是最后一行这一点方面还是有些疑惑