Getting minimum with a default [closed] - ruby

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In my params hash, I receive a value for :limit. I need to assign this value to a variable limit, but not exceed 50, and if the value for :limit is not given, default to 50.
First, I did this:
limit = [params[:limit], 50].min
This will make it maximum of 50, but won't work: If nothing is given for :limit, then this will make limit 0.
Next, I did this:
limit = [params[:limit] || 50, 50].min
This works, but I am wondering if this is efficient or if there's a better way to do this.

try
limit = (params[:limit] && params[:limit] < 50) ? params[:limit] : 50

Your solution it's quite easy to read but rather inefficient because you're creating an array and invoking the min method every time and it is an overkill.
A maybe less readable and more verbose but more easy on the machine approach could be:
if params[:limit] && params[:limit] < 50
params[:limit]
else
50
end

Related

Compare two links and choose the one with higher end number [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
So my problem is I try to compare two links from tumblr and choose that link which has higher number in this case is just choose _1280 over _500.
http://25.media.tumblr.com/393e9f295c4cac3af0a4b6d3a64c434d/tumblr_mi9e85iFwA1qavye5o1_500.jpg
http://25.media.tumblr.com/393e9f295c4cac3af0a4b6d3a64c434d/tumblr_mi9e85iFwA1qavye5o1_1280.jpg
I know how to get image links from tumblr but I'm too stupid to make, a code for this. I don't even know how to start... And this is just my beginning of trying do something in bash (cygwin).
I will appreciate any help :)
Here are some string manipulation functions to get you started.
bash:~$ var1=http://25.media.tumblr.com/393e9f295c4cac3af0a4b6d3a64c434d/tumblr_mi9e85iFwA1qavye5o1_500.jpg
bash:~$ var2=${var1##*_}
This gives you the part of the string after the last underscore
bash:~$ echo $var2
should output 500.jpg
bash:~$ var3=${var2%.*}
This gives you the part before the dot
bash:~$ echo $var3
should output 500
Then you can compare the numbers.

Octave : strange error when using ones [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I don't know why I have this strange error when using Octave on Windows 7 64 bit :
when I use ones(100:100) to declare a matrix 100x100. No problem. But when I use : ones(10:100) I will have error :
memory exhausted or requested size too large
And if I use ones(10:15) I must wait for long time (about 30 seconds) to have answer.
Please explain for me this error.
Thanks :)
Your syntax is wrong. To create an m x n matrix it should be:
ones(m,n)
e.g.
ones(100,100)
or
ones(10,15)

How to get a percentage from a range [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a basic math question. I am trying to get a percentage from a range of numbers and the algorithm is troubling me.
Say I have a range of -5 to +5 and I want to know what percentage is in between given a value. I know that -5 would be equivalent to 0% and 5 would be 100%, with 0 being 50%.
I tried to add 5 to bring up the scale, but it just feels like a hack. I would like it to feel dynamic so that I can give it any range and successfully work.
Ex.
percent = (5 + value) * 100 / 10
How do I figure out what value should be the a general case?
range = top_value - bottom_value
percent = (value - bottom_value) / range

create 10000 files with size 4k by using bash? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to do this.
Create 10000 files, (filename can be just combination of time and random number).
File size should be 4k.
And I want to time this. say how many seconds it will take.
How can I do this on bash?
Thank you.
time for x in {1..10000}; do
dd if=/dev/zero ibs=4k count=1 of=$x.txt 2>/dev/null
done

Comparing multiple values in an if statement - Ruby [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Can you compare multiple values in a single if statement in Ruby? If so, how? I know in other languages, you would just use a double ampersand (&&), but I have never seen any example or documentation about it, even in the official ruby documentation! Thanks for all the help in advance.
Yes, you can use the && operator
if a == 1 && b == 2
# do things
end
It IS in the documentation.
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UG

Resources