Select elements in array rails [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 5 years ago.
Improve this question
x = [1, 2, 3, 4]
x[1, x.size - 1]
#=> [2, 3, 4]
How do I rewrite this code to make it more elegant.
I would like something like
x[1, -1]

Array#[] accepts a Range instance, and the right boundary can be negative, specifying the amount of elements from the end:
x[1..-1]

Try this one instead.
x[1..-1]

Related

Generator with condition in F# [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 2 years ago.
Improve this question
if you know, how I can write generator with condition in F# - tell me please!) something like that:
let res = [for i in 1..5 if i % 2 = 0 then i]
printfn "%A" res
You were almost on the right track.
let res =
[
for i in 1..5 do
if i % 2 = 0 then
yield i
]
The feature you're looking for is list comprehensions.
This is similar to yield return in C#. The same comprehensions are available for seq and Array.

Please write out in simple English without too many key shortcuts like .pop or .include, etc., but I am completely lost as to how to do this [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 3 years ago.
Improve this question
Write a method strange_words that accept an array of strings The method should return an array containing all strings that are either shorter than 6 characters or begin with "e".
puts strange_words[
"taco", "eggs", "excellent", "exponential",
"artistic", "cat", "eat"
]
puts strange_words[
"elegant", "ellen", "monsterous"
]
Another way round would be to use regular expression.
strange_words = [
"taco", "eggs", "excellent", "exponential",
"artistic", "cat", "eat"
]
strange_words.select { |w| w[/\A[^e].{5,}/i] }
#⇒ ["artistic"]
strange_words.reject { |w| w[/\A[^e].{5,}/i] }
#⇒ all but `"artistic"`
The regular expression literally means “starts with anything but "e" followed by at least five characters (summing up to six+.)
a =
strange_words[
"taco", "eggs", "excellent", "exponential",
"artistic", "cat", "eat"
]
Code
puts a.select { |x| x.begin_with?'e' or x.length < 6 }

Ruby Sorting Array Alphabetically [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 have the Array which contains the mix data.
Its like
["AB_1020", "AB_950", "AB_50", "1000", "570"]
The output should be
AB_50, AB_950, AB_1020, 570, 1000
["AB_1020", "AB_950", "AB_50", "1000", "570"].sort_by do |k|
i, w = k.split('_').rotate
[w.to_s, -i.to_i]
end.reverse
#⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]
You can do something like this
p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
.flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}
ouptut
#⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]

Generate a random string with n digits number (except 0 at begin) 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 8 years ago.
Improve this question
Someone can teach me how to generate a random string with n digits number.
Ex: n=3, myString = "001" or "002" or ... "999" (except number 0 at begin)
p/s: I am using Ruby 1.8.7
n.times.map { (0..9).to_a.sample }.join
If it's for a password or something:
require 'securerandom'
random_number = SecureRandom.random_number(10**n)
formatted_number = "0#{random_number}"
Edit: If it doesn't need to be secure:
random_number = rand(10**n)
formatted_number = "0#{random_number}"

math.random function with step option? [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
A custom function that will return a random number with a step option available like in the for loop.
Example:
for i=1,10,**2** do
print(i)
end
Do you mean this:
function randomWithStep(first, last, stepSize)
local maxSteps = math.floor((last-first)/step)
return first + stepSize * math.random(0, maxSteps)
end
This gives the same behavior as math.random(first, last) except that the values will be "stepSize" apart. Note that the highest random # may not be "last", depends if (last-first) is a multiple of stepSize.

Resources