ruby入门

#vscode使用alt+shift+f format代碼,
# ctrl+shift+x 安裝Rufo-Ruby formatter和rufo (Ruby formatter)插件即可
# 安装code runner插件,ctrl + alt + N运行程序
# 安装Tabnine AI Autocomplete插件,实现自动补全

#-----------------------------------------------------------
#函数的定义和使用
def g(a = "world")
  puts "hello #{a}"
end

g(1)
g
# hello 1
# hello world

#-----------------------------------------------------------
#类的定义和使用
class Cat
  def initialize(name = "tom")
    @name = name
  end

  def eat(food = "fish")
    puts "1 #{@name} eats #{food}"
    puts '2 #{@name} eats #{food}'
    puts ("3 #{@name} eats #{food}")
    puts ('4 #{@name} eats #{food}')
  end

  def run
  end
end

Cat.new.eat
#puts可以成p,带不带括号没区别,#{变量名}在双引号里有效单引号里无效
# 1 tom eats fish
# 2 #{@name} eats #{food}
# 3 tom eats fish
# 4 #{@name} eats #{food}

c = Cat.new("jerry")
c.eat("dog")
# 1 jerry eats dog
# 2 #{@name} eats #{food}
# 3 jerry eats dog
# 4 #{@name} eats #{food}

#-----------------------------------------------------------
p c.respond_to?("eat")    #true
p c.respond_to?("fly")    #false
c.send("eat")             #call eat
# c.send("fly")       undefined method `fly

#-----------------------------------------------------------
# instance_methods(inherited_too = true) -> [Symbol][permalink][rdoc][edit]
# そのモジュールで定義されている public および protected メソッド名の一覧を配列で返します。

# [PARAM] inherited_too:
# false を指定するとそのモジュールで定義されているメソッドのみ返します。
#返回public和protected方法名。true会加上父类的,false只返回自己的
class Foo
  private def private_foo() end
  protected def protected_foo() end
  public def public_foo() end
end

# あるクラスのインスタンスメソッドの一覧を得る
p Foo.instance_methods(false)
p Foo.public_instance_methods(false)
p Foo.private_instance_methods(false)
p Foo.protected_instance_methods(false)
# [:protected_foo, :public_foo]
# [:public_foo]
# [:private_foo]
# [:protected_foo]

class Bar < Foo
end

class Bar
  protected def protected_bar() end
  public def public_foo() end
end

# あるクラスのインスタンスメソッドの一覧を得る。
# 親のクラスのインスタンスメソッドも含めるため true を指定して
# いるが、Object のインスタンスメソッドは一覧から排除している。
# 返回自己和父类的方法,这里减去了Object的方法
p Bar.instance_methods(true) - Object.instance_methods(true)
p Bar.public_instance_methods(true) - Object.public_instance_methods(true)
p Bar.private_instance_methods(true) - Object.private_instance_methods(true)
p Bar.protected_instance_methods(true) - Object.protected_instance_methods(true)
# [:public_foo, :protected_bar, :protected_foo]
# [:public_foo]
# [:private_foo]
# [:protected_bar, :protected_foo]

#-----------------------------------------------------------
class Dog
  attr_accessor :name, :age, :weight

  def initialize(name = "tom", height = 22, weight = 33)
    @name = name
    @height = height
  end
end

d = Dog.new
d.name = 1
d.age = 2
p d.respond_to?(:name)
p d.respond_to?(:name).to_s
p d.respond_to?(:height)
p d
# true
# "true"
# false
##<Dog:0x00000118e4066e38 @name=1, @height=22, @age=2>
# to_s是to_string,weight没有赋值不显示该字段
#p.height = 44            undefined method `height='
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值