Ruby what does star prefix means in a loop variable? [duplicate] - ruby

This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end

It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".

Related

Is there any way to write two values in one name in python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
f = open("dict.txt","a")
f.write("Customer_Name: "+Name+"\nMovies: "+Movies_Name+"\nQuantity: "+str(quantity)+"\n")
f.close()
Movies_Name is a list which might contain more than one movies. Now I want to write the names of the movies individually instead of writing them as a list.
For me it has nothing to do with discord itself. It is just Python question.
So answer can be found here
By using ''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
In your case:
f.write("Customer_Name: "+Name+"\nMovies: "+''.join(Movies_Name)+"\nQuantity: "+str(quantity)+"\n")

Ruby - code to determine the most common characterS(plural) in a string [duplicate]

This question already has an answer here:
How do I count the frequencies of the letters in user input?
(1 answer)
Closed 5 years ago.
Those of you who are marking this as a duplicate - seriously, try to be more responsible. The question that you have marked as the same is quite different to the question I've asked. I've already got a great answer that was not available on any of the questions that were asked about similar topics.
My Original Question:
I see that many have asked how to find the most common letter in a string. My code for that is below. But I'd like to know how to get multiple answers to return. That is to say, if there are several letters that have the same count as most common, how do I get my method to return those only and all of them?
def ltr(string)
results = string.scan(/\w/).reduce(Hash.new(0)) {|h,c| h[c] += 1; h}
sorted = results.sort_by{|key,value| value}
sorted[-1]
end
For example, if the string I input to this method is ("oh how i hate to get up in the morning")...the there are 4 each of the letters 'h', 'o', and 't'. My current method only returns the 't' with the count of '4'. How do I get the others to be returned as well?
Note: Please read questions carefully before you decide to mark them as duplicates. The question that was suggested as a possible duplicate simply shows how to count the frequencies of characters, not how to have it return only those which are the most common. Matt's answer is perfect.
You had a good answer. Just keep going thru the resulting array keeping only the elements where the count is as high as the count of the first item:
def ltr(string)
results = string.scan(/\w/).reduce(Hash.new(0)) {|h,c| h[c] += 1; h}
sorted = results.sort_by{|key,value| value}.reverse
top = sorted[0][1]
sorted.take_while{|key,value| value == top}
end

ruby 'best way' to generate 2d array [duplicate]

This question already has answers here:
How to declare an empty 2-dimensional array in Ruby?
(5 answers)
Closed 6 years ago.
I have create empty array of array like below , Is there any other best way to initialize it ?.
arr = [[],[],[],[],[],[],[],[],[]]
I think the best way to achieve it by using Array class.
ex:
Array.new(width){Array.new(height)}
you can also provide width & height value like width = 2 & height = 4
Have you tried something like
arr = Array.new(9) { Array.new }
?

Ruby chaining methods with if-statement [duplicate]

This question already has an answer here:
One line if statement in Ruby
(1 answer)
Closed 8 years ago.
I have the following:
def method(integer)
a = 3+integer
a += 10 if "one"<"another"
end
Can I write it in one line somehow with chaining methods?
Something like a = 3+f += 10 if "one"<"another"?
You could do it in one line using the ternary operator:
def method(integer)
a = integer + ("one"<"another" ? 13 : 3)
end
Make sure you don't hurt the readability of the code when you do that, though.
Since and or && both use short-circuit evaluation, you could use:
(a = 3+integer) and ("one"<"another") and (a += 10)
It says in 'Using “and” and “or” in Ruby':
and is useful for chaining related operations together until one of them returns nil or false
Another way of thinking about and is as a reversed if statement modifier
a= 3+ integer + ("one"<"another" ? 10 : 0)
3+ integer will add 3 to integer value and ("one"<"another" ? 10 : 0) will return 10 if condition is true otherwise will return 0.

Ruby ++ not work? [duplicate]

This question already has answers here:
No increment operator (++) in Ruby? [duplicate]
(3 answers)
Closed 9 years ago.
Using Ruby, I can't seem to get the following to work:
a = 1
a++
The above line works in irb but doesn't work when I compile from file.
Is there anything I missed out? I'm using Ruby 2.0.
Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1
Ruby doesn't have ++ or -- operators but += and -= accomplish the same thing. Try using the += notation like this:
a = 1
a+= 1
#=> 2
Here is a good reference list of valid ruby operators.

Resources