Variables in ruby method names
You can use #send method to call object's method by method's name:
object.send(:foo) # same as object.foo
You can pass arguments with to invoked method:
object.send(:foo, 1, "bar", 1.23) # same as object.foo(1, "bar", 1.23)
So, if you have attribute name in variable "attribute" you can read object's attribute with
object.send(attribute.to_sym)
and write attribute's value with
object.send("#{attribute}=".to_sym, value)
In Ruby 1.8.6 #send method can execute any object's method regardless of its visibility (you can e.g. call private methods). This is subject to change in future versions of Ruby and you shouldn't rely on it. To execute private methods, use #instance_eval:
object.instance_eval {
# code as block, can reference variables in current scope
}
# or
object.instance_eval <<-CODE
# code as string, can generate any code text
CODE
Update
You can use public_send
to call methods with regard to visibility rules.
object.public_send :public_foo # ok
object.public_send :private_bar # exception
How can Ruby tell the difference between variables and method names if they have the same scope?
variable would be first, but you can call method:
def foo
33
end
foo = 44
>foo
#=> 44
>foo() #also you can call it by: self.foo || My omission, thanks @CarySwoveland
#=> 33
On question
why?
@SergioTulentsev gave a good answer:
local variable has priority over bracketless method call because otherwise it wouldn't be possible to refer to the variable
Also, for more information about methods and variables you can read here
Rails - variable as part of method name
You could try using send
send("#{col_str}_greater_than".to_sym, 30)
Local variables and methods with the same name in Ruby?
In ruby you can have local variables and methods with the same name. This has some complications for example with setter methods in classes:
class Test
def active
@active
end
def active=(value)
@active = value
end
def make_active
active = true
end
end
t1 = Test.new
t1.active = true
t1.active #=> true
t2 = Test.new
t2.make_active
t2.active #=> nil
Code for t1 object will return expected result, but code for t2 returns nil, because make_active method is actually creating local variable and not calling active= method. You need to write self.active = true to make this work.
When you write gen_class, ruby tries to access local variable, if it is not defined ruby tries to call method. You can call your method explicit by writing gen_class().
Method and variable name is the same
Try this:
puts hello()
Use variable method names in rails
I think what you're looking for is method_missing
- take a look for example at these articles.
method's local variable with same name as another method
I think that the local variable is declared as soon as it's enunciated. In ruby the lookup is first to look for a local variable, if it exists it's used, and if not it looks for a method. This would mean that val = val declares the first val as local and the left-hand val then matches it (not sure about it I should check the ruby under microscope to be sure)
If you try
class A
def val
10
end
def test
back = []
x = val
back << x
val = x + 1
back << val
x = val
back << x
end
end
p A.new.test
then all is good, it prints [10, 11, 11] which means the first x = val calls the method, the second calls the local variable, presumably.
Rails - How to use variable as part of method name
Use Object.send
to call method by name. For example,
def my_action(state)
if [:start, :end].include?(state)
model.send("method_#{state}")
end
end
Make sure to validate state
variable for security. Object.send
can call any method including private ones.
naming methods as variables calling methods Ruby
so my question is: how can you assign a variable calling a method as a
method name? It feels like I'm missing something..
You don't. In this code
def name=(name)
@name = name
end
name=
isn't a variable name
calling a method =
. The name of the method is name=
.
Edit:
In the above code snippet the def
paired with a terminating end
constitutes a method definition.
def method_name(param1, param2)
# method body
end
On the same line as def
there can only be the method name, optional parentheses, and the param list. By definition having a "variable calling a method" in that line would be illegal. So in your code name=
is the method name.
Related Topics
Editing JSON Array Contents in Ruby
Adding Nested Attributes to Devise User Model
Ruby: Append Text to the 2Nd Line of a File
Illegal Quoting in Line 1 Using Ruby CSV
Local Server Error After Upgrading Ruby from 1.8.7 to 1.9.2 (With Rails 3.1.1)
Print Unicode Escape Codes from Variable
Can't Run a Ruby Hello World Application in Aptana
How to Mimic Browser X509 Client Certificate Verification Without Access to Http Layer
How to Translate Arabic/Persian Numbers to English Using Ruby
Routing Error During "Ruby on Rails-Tutorial"
Replicating Xml Request with Savon/Ruby
Sum of Arrays of Different Size