Remove trailing commas in ruby string [closed] - ruby-on-rails

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
I am getting the following value in my params: "45,284"
How can I remove the the leading and trailing double quotes from my string?
The output that I should get is: 45,284

You can do this to your params[:userValues] to make it suitable for IN clause in your query
"45,284".split(",").map(&:to_i) #=> [45, 248]
So for params[:userValues] it will be
user_values = params[:userValues].split(",").map(&:to_i)
Now the query will look like this
#user = User.where('is_active = ? and is_support_user = ? and id IN (?)', true, false, user_values).order(:user_name)
This will work, try it out

class String
def trim
self.gsub!(/\A"|"\Z/, '')
end
end
"\"45,284\"".trim

Related

How to exclude special characters in ruby [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 10 months ago.
Improve this question
I am new to ruby and looking for a solution where I wanted to remove search a lines in a file with specific string and that string does not contains special characters.
eg:
irb(main):005:0> File.readlines("test.sh").select {|line| line =~ /Passphr|passphrs/}
=> ["passphrs=mw4test\n", "Passphrase=$passphrs\n", "Passphrase=$passphrs\n"]
I want ignore the text which is having "=$" as combination and desire output should be as:
passphrs=mw4test
Please suggest?
Perhaps just add a second condition to your select block:
File
.readlines("test.sh")
.select { |line| line =~ /Passphr|passphrs/ && !line.include?('=$') }

Checking multip hash keys if present [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
Any tips to refactor this condition?
params = {:p1=>"foo", :p2=>true, :p3=>nil, :pN=>""}
if params[:p1].present? && params[:p2].present? && params[:pN].present?
# do something...
Something like this:
params.values_at(:p1, :p2, :pN).all?(&:present?)
if params.values_at(*%i[p1 p2 p3]).all?(&:present?)
values_at returns an Array of the values for each key you provide.
all? is true iff the predicate (present?) is true for every member of the enumerable. values_at will include nil for a missing key (rather than omit it), so you don't need to worry about the array collapsing down to only present values.
keys = [:p1, :p2, :pN]
puts "hi" if keys.all? { |k| params[k].present? }
This has the advantage of terminating the hash lookup as soon as params[k].present? is false.

Change resource into arrays [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
Is there a way to change resources into array?
For example:
#video = Video.all
to
[["Test", 1],["Test2", 2],["Test3", 3]]
I tried #video.to_a but it didn't work
By resources you mean an ActiveRecord collection?
If so, it can be something like
#video.map{ |video, index| [video.name, index+1] }
You can use ActiveRecord::Calculations#pluck to select one or more attributes:
Video.pluck(:name, :id)
#=> [["foo", 1], ["bar", 2], ["baz", 3]]

Rails: list column name if true [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've got a pretty standard Preferences table. I'd like to list all true booleans for each record therein. How would I go about that? I know that Preference.column_names will give me all, but I need each record's particular true settings. Any idea? Something like
#preference.column_names do |c|
c if c = true
end
Thanks!
Here's the general idea of looping through an objects attributes with the attribute name and value. Are you having to filter out only boolean fields? Or are all the fields boolean?
#preference.attributes.each do |attr_name, attr_value|
"#{attr_name} is #{attr_value}" if attr_value == true
end
First you'll need to know the boolean columns, something like this should give you their names:
booleans = Model.columns.select { |c| c.type == :boolean }.map(&:name)
Then you can use send to extract the values based on a name and a simple "is it true" test takes care of the rest:
trues = booleans.select { |name| #preference.send(name) == true }

Ruby: Dynamically build member name of a struct? [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
I have two questions.
1) I need to create a dynamic structure, whose members are driven through an array.
For ex:
members = [:a, :b]
Config = Struct.new(members) #=> Struct.new(:a, :b)
FlatConfig = Struct.new(members) #=> Struct.new(:a, :b)
config = Config.new()
flat = FlatConfig.new()
After some days, If I need to add another member to these struct, then all I need to do is add a member in that members array (memebers = [:a, :b, :c]) and I don't need to tough the code further. Thus I am asking this. How to achieve this?
2) Now I need to build the values of flat Struct members by means of doing some manipulation on config struct member values.
For eg:
config.each{|configMember|
result = configMember.collect{|c| someArray.collect{|s| s + '--' + y}}
flat[":#{cofigMember}"] = result #=> Intent is to store result in same member as iterated through config struct.
}
How to achieve (1) and (2)?
Thanks in advance.
For your 1), use the splat operator:
Struct.new(*members)
I don't really understand your second question. Is "how can I access the same variable in two configs"?
config.members.each { |key|
configMember = config[key]
# do something
flat[key] = result
}

Resources