String replace to method [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to replace a localized string to localized method.
From:
"social_1.localized()"
To:
"social_1".localized()
What is the best way to do?

May be this:
"social_1.localized()".gsub(".localized()","").localized()
or
my_string, my_method = "social_1.localized()".split('.')
my_method = my_method.gsub!("()",'').to_sym
my_string.send(my_method)
#uri-agassi (see comment) is right. using send this way may be a security risk. especially if it comes from user input (i.e. from the params object). you could think about to whitelist callable methods:
if [:upcase, :downcase, :capitalize].include?(my_method)
my_string.send(my_method)
end
Or at least ask the object, that it knowns the method to call:
my_string.send(my_method) if my_string.respond_to?(my_method)

Related

What is this specific method called? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm curios as to what this specific method is called:
public Word(String word) {
this._word = word;
}
Thanks in advance!
This is called a constructor. In this case, it would be for a Word object. It takes parameters to assign values to instance variables (in this case, the String word.)
Here is a link to the Oracle documentation on providing constructors in Java for further reference.

Ruby objects without 'to_s' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Are there any objects in ruby that don't respond to to_s?
The question isn't meant to ask whether it is possible to create one, which I know can be done by undef_method. Feel free to explain details, including caveats of undefining.
The BasicObject class does not define a to_s method, so any instance of that class would not have a to_s method.
We should never specialize a subclass, since the subclass would not attend to the parent expectation anymore, have a look in the Liskov Substitution Principle.

When creating a DSL in Ruby, is it possible to create an alternate version of `def`? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
It is possible to call define_method directly or from within a method call that accepts a block. But is it possible to create a new method or keyword that performs manipulation on code and then declares it into a method, but has the same syntax as the built-in def?
No. You can define a method, but you cannot define a keyword. Whatever method you define on the Module class, you cannot pass a method body to it without the do keyword, unlike with def.

Is it possible to pass parameter as argument of a method like this in Ruby [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to pass parameter to a method like this:
variable = my_method(:parameter)
No quotes, no nothing.Just -> :parameter .
Yes, you can pass a symbol to a method. Example:
puts('hello')
or
puts(:hello)

Ruby 1.9, return array if keys include a particular object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Code:
#albums = #genres.each_with_index { |item,key|
if item.keys.include?('Albums')
break
end
}
This should be returning the Albums array (the #genres object is a huge multidimensional JSON response)
I reckon this is what I get for trying to code while being sick... or just simply doing things wrong... either way, any help is much appreciated!
I think you want #detect (or its synonym #find):
#albums = #genres.detect { |item| item.key?('Albums') }['Albums']
EDIT | Also note that you can provide an argument to break just like you can do with return, if you want to break and return a specific value.

Resources