ruby中如何将参数传递给别名方法

发布于 2024-09-15 17:23:56 字数 737 浏览 5 评论 0原文

我想在 ruby​​ 中创建一个别名方法并将参数传递给它。我设法做了以下事情

class User
  def say_name
    puts "I'm sameera"
  end
end

class User
  alias :tell_name :say_name
  def say_name
    puts "I'm sameera gayan"
    tell_name
  end
end

user = User.new
user.say_name

,它给了我输出,因为

我是 Sameera gayan 我是 Sameera

但现在我想将我的名字作为参数传递给第一个“say_name”方法。所以新代码将类似于

class User
  def say_name(name)
    puts "#{name}"
  end
end

class User
  alias :tell_name :say_name(name)
  def say_name(name)
    puts "I'm sameera gayan"
    tell_name(name)
  end
end

user = User.new
user.say_name("my new name")

但现在这不起作用(将参数传递给别名方法)。所以我的问题是如何将参数传递给别名方法。

我希望你清楚这个问题。预先感谢

欢呼

Sameera

I want to create a alias method in ruby and pass parameters to this. I managed to do the following

class User
  def say_name
    puts "I'm sameera"
  end
end

class User
  alias :tell_name :say_name
  def say_name
    puts "I'm sameera gayan"
    tell_name
  end
end

user = User.new
user.say_name

and it gives me the out put as

I'm sameera gayan
I'm sameera

But now i want to pass my name as a parameter to the first 'say_name' method. So the new code will be like

class User
  def say_name(name)
    puts "#{name}"
  end
end

class User
  alias :tell_name :say_name(name)
  def say_name(name)
    puts "I'm sameera gayan"
    tell_name(name)
  end
end

user = User.new
user.say_name("my new name")

But now this doesn't work (passing parameter to alias method). So my question is how to pass parameters to an alias method.

I hope this question is clear to you. Thanks in advance

cheers

sameera

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北座城市 2024-09-22 17:23:56

我尝试了这个并得出了这个解决方案

class User

        def say_name(name)

              puts "#{name}"

        end

end

class User

        alias :tell_name :say_name

        def say_name(name)

              puts "Hi"

              tell_name(name)

        end

end

user = User.new

user.say_name("Rohit")

之所以有效,是因为我们无法将参数传递给别名。这就是你想要做的。

I tried this one and came to this solution

class User

        def say_name(name)

              puts "#{name}"

        end

end

class User

        alias :tell_name :say_name

        def say_name(name)

              puts "Hi"

              tell_name(name)

        end

end

user = User.new

user.say_name("Rohit")

The reason this is working because we cannot pass arguments to aliases. And that is what you were trying to do.

不语却知心 2024-09-22 17:23:56

我正在学习 Ruby。所以当我看到这个问题时我决定尝试一下。尽管我还没有详细了解别名方法,但我遇到了一个解决方案。不知道这是不是应该这样做。还不能说为什么会这样。可能在我深入学习后几天我会添加它。目前,这是一个可行的解决方案。

class User
  attr_accessor :name
  def say_name
    puts "#{name}"
  end
end

class User
  alias :tell_name :say_name
  def say_name
    puts "I'm sameera gayan"
    tell_name
  end
end

user = User.new
user.name = "Sameera"
user.say_name 

这个 qtn 也有帮助我在这个过程中。

I'm learning Ruby. So when I saw this question I decided to try it. Though I have yet to learn about aliasing methods in detail I came across a solution. Don't know if it is the way it should be done. And can't say why it is so yet. May be in a few days after I have learn't in depth I'll add it. For now, here is a working solution.

class User
  attr_accessor :name
  def say_name
    puts "#{name}"
  end
end

class User
  alias :tell_name :say_name
  def say_name
    puts "I'm sameera gayan"
    tell_name
  end
end

user = User.new
user.name = "Sameera"
user.say_name 

This qtn also helped me in the process.

少女七分熟 2024-09-22 17:23:56

我粘贴了一些可以帮助您的别名方法组合

class Test
  def simple_method
    puts "I am inside 'simple_method' method"
  end

  def parameter_instance_method(param1)
    puts param1
  end

  def self.class_simple_method
    puts "I am inside 'class_simple_method'"
  end

  def self.parameter_class_method(arg)
    puts arg
  end


  alias_method :simple_method_new, :simple_method

  alias_method :parameter_instance_method_new, :parameter_instance_method

  singleton_class.send(:alias_method, :class_simple_method_new, :class_simple_method)
  singleton_class.send(:alias_method, :parameter_class_method_new, :parameter_class_method)
end

Test.new.simple_method_new
Test.new.parameter_instance_method_new("I am parameter_instance_method")

Test.class_simple_method_new
Test.parameter_class_method_new(" I am parameter_class_method")

输出

I am inside 'simple_method' method
I am parameter_instance_method
I am inside 'class_simple_method'
I am parameter_class_method

I am pasting some combination of alias method that can help you

class Test
  def simple_method
    puts "I am inside 'simple_method' method"
  end

  def parameter_instance_method(param1)
    puts param1
  end

  def self.class_simple_method
    puts "I am inside 'class_simple_method'"
  end

  def self.parameter_class_method(arg)
    puts arg
  end


  alias_method :simple_method_new, :simple_method

  alias_method :parameter_instance_method_new, :parameter_instance_method

  singleton_class.send(:alias_method, :class_simple_method_new, :class_simple_method)
  singleton_class.send(:alias_method, :parameter_class_method_new, :parameter_class_method)
end

Test.new.simple_method_new
Test.new.parameter_instance_method_new("I am parameter_instance_method")

Test.class_simple_method_new
Test.parameter_class_method_new(" I am parameter_class_method")

OUTPUT

I am inside 'simple_method' method
I am parameter_instance_method
I am inside 'class_simple_method'
I am parameter_class_method
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文