Difference between @instance

I Just started learning ruby and I don't see the difference between an @instace_variable and an attribute declared using attr_accessor . What is the difference between the following two classes: class MyClass @variable1 end and class MyClass attr_accessor :variable1 end I searched lot of tutorials online and everybody uses different notation, Does it have to do anything with the

@instance之间的区别

我刚开始学习ruby,并没有看到@instace_variable和使用attr_accessor声明的属性之间的attr_accessor 。 以下两个类别有什么区别: class MyClass @variable1 end 和 class MyClass attr_accessor :variable1 end 我在网上搜索了很多教程,每个人都使用不同的符号,它是否需要对ruby版本做任何事情? 我也在StackOverflow中搜索了几个旧线程 Ruby中的attr_accessor是什么? 这两个Ruby类初始化定义有什么区别?

reader of the Module calss in Ruby?

This question already has an answer here: What is attr_accessor in Ruby? 18 answers It works like this: module Attr attr_accessor :my_variable end class MyClass @my_variable = "hi" def initialize @my_variable = "ho" end end You include the module in the class to construct an accessor for the instance variable @my_variable : MyClass.include Attr c = MyClass.new c.my_variable

读者在Ruby模块calss?

这个问题在这里已经有了答案: Ruby中的attr_accessor是什么? 18个答案 它是这样工作的: module Attr attr_accessor :my_variable end class MyClass @my_variable = "hi" def initialize @my_variable = "ho" end end 您include在该类中include该模块以构建实例变量@my_variable : MyClass.include Attr c = MyClass.new c.my_variable #=> "ho" c.my_variable = "huh?" #=>

Getting local variable names defined inside a method from outside the method

Is it possible in Ruby to get all the local variables names defined inside a method from outside the method using metaprogramming? def foo var = 100 arr = [1,2] end Something like foo.local_variables. I only need to retrieve the variable names, not their values. Why I'm trying to find those variable names : I have a class where I want to dynamically generate setters for all instance

从方法外部获取在方法内定义的局部变量名称

是否有可能在Ruby中使用元编程从方法外部获取在方法内定义的所有局部变量名称? def foo var = 100 arr = [1,2] end 像foo.local_variables。 我只需要检索变量名称,而不是它们的值。 为什么我试图找到这些变量名称 :我有一个类,我想动态地为所有在“#initialize”中初始化的实例变量生成setter。 为了做到这一点,在“初始化”内部,我使用了类似instance_variables.each do |inst_var_name| define_singleton_metho

method use case too complex?

I've been bashing my head against this for about three days now. I've created a class that models html pages and tells cucumber step definitions where to populate form data: class FlightSearchPage def initialize(browser, page, brand) @browser = browser @start_url = page #Get reference to config file config_file = File.join(File.dirname(__FILE__), '..', 'config', 'sit

方法用例太复杂?

现在我已经三天左右对抗这个问题了。 我创建了一个模拟HTML页面的类,并告诉黄瓜步骤定义填充表单数据的位置: class FlightSearchPage def initialize(browser, page, brand) @browser = browser @start_url = page #Get reference to config file config_file = File.join(File.dirname(__FILE__), '..', 'config', 'site_config.yml') #Store hash of config values in local variable config

Setting variable A with name stored in variable B

I have the following two variables: a = 1; b = 'a'; I want to be able to do SOMETYPEOFEVALUATION(b) = 2; so that the value of variable a is now set to 2 . a # => 2 Is this possible? Specifically, I am working with the Facebook API. Each object has a variety of different connections (friends, likes, movies, etc). I have a parser class that stores the state of the last call to the Fac

设置名称存储在变量B中的变量A.

我有以下两个变量: a = 1; b = 'a'; 我希望能够做到 SOMETYPEOFEVALUATION(b) = 2; 所以变量a的值现在设置为2 。 a # => 2 这可能吗? 具体来说,我正在使用Facebook API。 每个对象都有各种不同的连接(朋友,喜欢,电影等)。 我有一个解析器类,用于存储所有这些连接的最后一次调用Facebook API的状态。 这些状态都被命名为与您必须调用以更新它们的GET相对应的状态。 例如,要更新音乐连接,请使用https:

Checking if a variable is defined?

How can I check whether a variable is defined in Ruby? Is there an isset -type method available? Use the defined? keyword (documentation). It will return a String with the kind of the item, or nil if it doesn't exist. >> a = 1 => 1 >> defined? a => "local-variable" >> defined? b => nil >> defined? nil => "nil" >> defined? String => "cons

检查一个变量是否被定义?

我如何检查一个变量是否在Ruby中定义? 是否有可用的isset type方法? 使用defined? 关键字(文档)。 它会返回一个String类型的项目,如果不存在,则返回nil 。 >> a = 1 => 1 >> defined? a => "local-variable" >> defined? b => nil >> defined? nil => "nil" >> defined? String => "constant" >> defined? 1 => "expression" 正如skalee所评论的那样

Get the name of the currently executing method

$0是顶级Ruby程序的变量,但是对于当前的方法是否有一个? Even better than my first answer you can use __method__: class Foo def test_method __method__ end end This returns a symbol – for example, :test_method . To return the method name as a string, call __method__.to_s instead. Note: This requires Ruby 1.8.7. 从http://snippets.dzone.com/posts/show/2785: module Kernel private de

获取当前正在执行的方法的名称

$0是顶级Ruby程序的变量,但是对于当前的方法是否有一个? 甚至比我的第一个答案更好,你可以使用__method__: class Foo def test_method __method__ end end 这会返回一个符号 - 例如:test_method 。 要将方法名称作为字符串返回,请__method__.to_s调用__method__.to_s 。 注意:这需要Ruby 1.8.7。 从http://snippets.dzone.com/posts/show/2785: module Kernel private def this_method_name call

method vs. def

As a programming exercise, I've written a Ruby snippet that creates a class, instantiates two objects from that class, monkeypatches one object, and relies on method_missing to monkeypatch the other one. Here's the deal. This works as intended: class Monkey def chatter puts "I am a chattering monkey!" end def method_missing(m) puts "No #{m}, so I'll make one..." de

方法与def

作为编程练习,我编写了一个创建类的Ruby片段,实例化该类中的两个对象,monkeypatches一个对象,并依靠method_missing来对另一个对象进行monkeatch。 这笔交易。 这按预期工作: class Monkey def chatter puts "I am a chattering monkey!" end def method_missing(m) puts "No #{m}, so I'll make one..." def screech puts "This is the new screech." end end end m1 = Monkey.new m2

Why do we need attr

I fail to understand why we needed the attr_reader and attr_writer, or attr_accessor to be declared in a class. I read both this and this posts, but those posts explain mainly how they work, not as much as why they are there. In case of class Person attr_accessor :age end bob = Person.new bob.age = 99 bob.age It seems a little redundant having to tell Ruby to write and read age, while not

为什么我们需要attr

我不明白为什么我们需要在类中声明attr_reader和attr_writer或attr_accessor。 我阅读了这篇文章和这篇文章,但这些文章主要解释了他们的工作方式,而不是为什么他们在那里工作。 的情况下 class Person attr_accessor :age end bob = Person.new bob.age = 99 bob.age 看起来有点多余,不得不告诉Ruby编写和读取年龄,而不能自动在类之外编写和读取实例变量。 为什么我们需要在Class中设置读写器而不是下面的代码并保

if VARIABLE in ARRAY ruby

I thought this should work: def setMethod(method) if method in @@methods ... do something end end But I keep getting keyword error for in 试试这个,假设@@methods是一个数组: if @@methods.include?(method) # ... end

如果VARIABLE在ARRAY ruby​​中

我认为这应该工作: def setMethod(method) if method in @@methods ... do something end end 不过,我不断收到错误关键字用于in 试试这个,假设@@methods是一个数组: if @@methods.include?(method) # ... end