This question already has answers here:
Call a method by name [duplicate]
(4 answers)
Closed 8 years ago.
I'm trying to pass min and then later max to a method, but it doesn't seem to work.
def worker(x)
[3,4].x
end
worker(min)
Do you want call methods by name? Then, use Object#send.
def worker(x)
[3,4].send(x)
end
worker(:min)
# => 3
worker(:max)
# => 4
Related
This question already has answers here:
Converting string from snake_case to CamelCase in Ruby
(10 answers)
Closed 6 years ago.
I want to turn string into CamelCase fashion In Ruby. The question also applies to words with underscores.
For example:
"human" => "Human"
"little_human" => "LittleHuman"
How can I do this?
With regexp:
def camelize(str)
str.gsub(/(^.)|(_.)/) { |l| l[-1].upcase }
end
In rails there is a camelize method. In ruby you can write the method on your own. Something like
def camelize(s)
s.downcase.split('_').map(&:capitalize).join
end
This question already has answers here:
How to dynamically create a local variable?
(4 answers)
Closed 3 years ago.
Not sure what am I doing wrong here or missing.
avar = 'test'
test_bvar = 'passed'
finalanswer = send "#{avar}_bvar"
puts " #{finalanswer}"
thanks,
The send method is used to call a method by name programmatically. Since your test_bvar is not a method but a local variable, you need to refactor it like this:
def test_bvar
"passed"
end
avar = "test"
finalanswer = send "#{avar}_bvar"
puts " #{finalanswer}"
Then you will reach your aim.
You are defining test_bvar as a local variable:
test_bvar = 'passed'
and you're trying to call it like a method:
send "test_bvar"
If you want to make this a method, do as #Mack94's answer suggests.
This question already has an answer here:
Ruby x,y = gets.split.map &:to_i
(1 answer)
Closed 7 years ago.
I am looking at some ruby code.
But am unsure what is happening on this line:
n,1 = gets.split.map &:to_i
I understand some input array of strings is being mapped to an array of integers but why is that being assigned to n,1?
That line of code doesn't do anything, it's simply a SyntaxError. You can't assign to 1, that's not a legal name for a local variable.
This question already has answers here:
What does !! mean in ruby?
(8 answers)
Closed 9 years ago.
What does the !! mean in this Ruby function?
def is_i?
!!(self =~ /^[-+]?[0-9]+$/)
end
It makes sure the response is a boolean. So nil or false wil become false, any other value becomes true
This question already has answers here:
How to call methods dynamically based on their name? [duplicate]
(5 answers)
Closed 8 years ago.
Regardless of whether it's good practice or not, how can I dynamically call accessor methods in Ruby?
Here's an example class:
class Test_Class
attr_accessor :a, :b
end
I can use the Object.send method to read the variable...
instance.a = "value"
puts( instance.send( "a" ) )
# => value
But I'm having a hard time trying to write to it. These throw "wrong number of arguments (1 for 0) (ArgumentError)"
instance.send("a", "value")
and
instance.method("a").call("value")
Please help me StackOverflow!
I am not a ruby expert, but I think that you could do:
instance.send("a=", "value")
You can also directly access instance variables of an object using instance_variable_* functions:
instance = Test_Class.new # => #<Test_Class:0x12b3b84>
# instance variables are lazily created after first use of setter,
# so for now instance variables list is empty:
instance.instance_variables # => []
instance.instance_variable_set(:#a, 123) # => 123
instance.a # => 123
instance.instance_variables # => ["#a"]
instance.instance_variable_get("#a") # => 123