Ruby使用默认值将部分参数传递给函数

Ruby使用默认值将部分参数传递给函数,ruby,rspec,capybara,Ruby,Rspec,Capybara,我不熟悉Capybara、rspec和Ruby,我有一个函数来填写表单: def form_fill(name:'James', age:'25', pets:'cat') fill_in 'Name', with: name fill_in 'Age' , with: age fill_in 'Pets', with: pets end 我想知道该在函数中做些什么更改,以便再次使用相同的函数修改表单(我已经填写了表单) 例如: 我填写了表格(姓名:'Bob'),现在我

我不熟悉Capybara、rspec和Ruby,我有一个函数来填写表单:

def form_fill(name:'James', age:'25', pets:'cat')
    fill_in 'Name', with: name
    fill_in 'Age' , with: age
    fill_in 'Pets', with: pets
end
我想知道该在函数中做些什么更改,以便再次使用相同的函数修改表单(我已经填写了表单)

例如: 我填写了
表格(姓名:'Bob')
,现在我的表格是:

Name   Age    Pets
----   ----   ----
Bob    25     cats
稍后我想更改相同的保存表单,并且只通过调用相同的函数来更改年龄,该函数的参数为age:form_fill(age:45)

此时将使用默认值将表单更改为:

Name   Age    Pets
----   ----   ----
James  45     cats

因此,我想知道如何同时实现与填充和修改器相同的功能。

看起来您只需要在这里使用一个普通的旧Ruby对象类。首先,我将创建一个Person类,您将使用该类设置有关Person的属性

class Person
  attr_accessor :name, :age, :pets

  def initialize(name: "James", age: 45, pets: "cat")
    @name = name
    @age = age
    @pets = pets
  end
end
这将允许您执行以下操作:

person = Person.new(name: "Bob")
=> #<Person:0x007fac4bb27128 @age=45, @name="Bob", @pets="cat">
如果要修改此人,请执行以下操作:

person.age = 25
form_fill(person)

希望这有帮助

看起来您只需要在这里使用一个普通的老Ruby对象类。首先,我将创建一个Person类,您将使用该类设置有关Person的属性

class Person
  attr_accessor :name, :age, :pets

  def initialize(name: "James", age: 45, pets: "cat")
    @name = name
    @age = age
    @pets = pets
  end
end
这将允许您执行以下操作:

person = Person.new(name: "Bob")
=> #<Person:0x007fac4bb27128 @age=45, @name="Bob", @pets="cat">
如果要修改此人,请执行以下操作:

person.age = 25
form_fill(person)

希望这有帮助

看起来您只需要在这里使用一个普通的老Ruby对象类。首先,我将创建一个Person类,您将使用该类设置有关Person的属性

class Person
  attr_accessor :name, :age, :pets

  def initialize(name: "James", age: 45, pets: "cat")
    @name = name
    @age = age
    @pets = pets
  end
end
这将允许您执行以下操作:

person = Person.new(name: "Bob")
=> #<Person:0x007fac4bb27128 @age=45, @name="Bob", @pets="cat">
如果要修改此人,请执行以下操作:

person.age = 25
form_fill(person)

希望这有帮助

看起来您只需要在这里使用一个普通的老Ruby对象类。首先,我将创建一个Person类,您将使用该类设置有关Person的属性

class Person
  attr_accessor :name, :age, :pets

  def initialize(name: "James", age: 45, pets: "cat")
    @name = name
    @age = age
    @pets = pets
  end
end
这将允许您执行以下操作:

person = Person.new(name: "Bob")
=> #<Person:0x007fac4bb27128 @age=45, @name="Bob", @pets="cat">
如果要修改此人,请执行以下操作:

person.age = 25
form_fill(person)
希望这有帮助