How does find method work in Ruby [closed] - ruby

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 7 years ago.
Improve this question
I understood the working of find method i.e. looks for the first occurrence. But I don't understand how find method is implemented at class level. Can somebody explain me the flow of this below written code? Also explain me the relation between find method and yield. How the values are passed between yield call and find call? Also explain what is the use of nil. Thanks.
class Array
def find
each do |value|
return value if yield(value)
end
nil
end
end
[1,3,5,7,9].find {|v| v*v > 30}

First of all; you know how yield works right??
{|v| v*v>30}
every item in the array is passed to this block as param v = every element
Explaination:
return value if yield(value) signifies that the current item of the array is returned from the find method if the block passed return true for the any item of the array.
So, it only returns single item; actually the first item matching the condition; unlike select method that returns every item matching the condition.
2.2.1 :030 > [1,2,3].select{|x| x>1}
=> [2, 3]
Moreover; the each do |value| iterates over the the current/self instance of Array class

Related

How to get args when call function with square brackets in the 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 3 years ago.
Improve this question
I am the beginner of Ruby.
I get one problem when I read the Ruby code.
There have one function with square-brackets.
How can I get the args from that function?
Here is the Class
class Student
class << self
def count
end
end
end
Here is the function request.
Student.count["Jack"]
Square brackets have no special meaning in Ruby. It is rather a method #[] called on the receiver.
Amongst many others, this method is noticeably declared by Array, Hash and Proc. Because of the parameter passed to #[], which is "Jack" string, it is most likely either Hash or Proc.
That said, it depends on what is returned by Student::count.
Hash example
def count
{"Jack" => 1, "Mary" => 2}
end
count["Jack"]
#⇒ 1
Proc example
def count
->(name) { "Hi, #{name}!" }
end
count["Jack"]
#⇒ "Hi, Jack!"

Hi just a newbie ruby programmer here wanting to understand a few things [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 6 years ago.
Improve this question
I'm trying to see if each position on a board is taken by this line of code. Can someone explain to me why this works?
def full?(board)
**board.all?{|position| position == "X" || position == "O"}
end**
And to see who is the winner, can someone explain to me the mechanics of the lines with the stars.
def winner(board)
****if winning_combo == won?(board)
board[winning_combo.first]****
end
end
We can't tell exactly what the structure of board is from the example you provided but it seems to be some Enumerable.
Take a look at Enumerable.all?
all? [{ |obj| block } ] → true or false
Passes each element of the collection to the given block. The method returns true if the block never returns false or nil.
So board.all?{|position| position == "X" || position == "O"} tests each element of board to see if it is either "X" or "O" and full? will return false if any position has not yet been set to one of those values.
We have no way to know what winning_combo is from the code provided or exactly what the won? function does. winning_combo.first might return an index, in that case if board is an Array then board[winning_combo.first] would get one element of the board. That seems reasonable in this case since from full? we see that the elements are "X" or "O" so winner would then return one of those characters.

What does `each` method return? [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
When I call
#data.each do |d|
some code here
end
what does the each method return? An array of objects? Or more generally, what does do...end block take?
It iterates over the members of the enumerable object. So if #data is an array it will return the elements of the array one at a time as d. Exact behavior depends on the type of object. See the docs
Books.all returns a object, that although it is not an array, behaves like one in this case.
each returns the receiver, in this case #data.
The do ... end is a block.
The each method will run the block (using the method yield) once for each value in data. This is called an iterator. For an array it will iterate over all the values in the array, for a hash it will run once per every pair of (key,value). You can define your own iterator for your own classes.
The return value of each depends on the object it is invoked. When invoked on an array it will return the array.
In your case the method each will execute the block once per each value and send each value as a parameter to the block.
For example-
a = [1,2,3]
a.each do |n|
p n
end
is just the same as
p a[0]
p a[1]
p a[2]

Problems with loops in Ruby Code [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
Hey I have an array with numbers in it.
Now I want to divide the value at 17th position of the array by the value at the first position of the array, then the 18th by the second, and so on. The results should build a new array.
Then I want to scan all values of the new array and if two or more successive values are bigger than 1.2, I want to add the result of dividing the first by the last value of that row for all of successive values. If one value is 1.2 and the next for example 0.8, the values of the array should not be changed.
Here is my code:
a = [1,2,3,4,5,9,5,13,14,17,19,23,19,34,46,12,13,45,46,67,78,79]
b = Array.new
c = Array.new
a.each_cons(18) { |c| b.push(c[17]/c[0] }
Do you have an idea how to implement the condition?
I think this will do it, although I selectively interpreted some things from your question. Specifically, in "of that row for all of successive values," does "row" refer to the sliding block from each_cons? Ditto for "all of successive values."
catch (:done) do
for i in 2..b.length do
b.each_cons(i) do |d|
for j in 2..d.length do
d.each_cons(j) do |g|
if g.all? { |g| g > 1.2 }
c = b.map { |f| f + (d[0].to_f/d[i-1].to_f) }
break
end
if !c.empty? then throw :done end
end
end
end
end
end
puts c

Tricky operators 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 9 years ago.
Improve this question
I know Ruby has a bunch of useful operators, like ||=
What other tricky operators does it have?
I haven't found any references for it.
I find that the splat operator is one of the trickiest Ruby operators:
It splits arrays:
a,b,c = *[1,2,3]
Or builds an array:
*a = 1,2,3
It can also be used in case statement:
first = ["one", "two"]
second = ["three", "four"]
case number
when *first
"first"
when *second
"second"
end
It can be used as function argument for varargs:
def stuff *args
args.join('|')
end
As it is used for both (splitting and creating arrays), I always have to check the syntax before using it. It can be used for so many purposes (like converting a hash to an array) that I really find it hard to master.
The ampersand at the end of a method signature will grab and expect a block for you.
def foo(bar, &block)
block.call (bar += 1)
end
The ampersand can also be used in this form to call to_proc and let you call the :address method with a symbol (example is borrowed from elsewhere)
#webs ||= Web.find(:all).index_by &:address
The shortcuts like += and -= are handy.
Not an operator, so much as another shortcut Rails makes possible. This will get you bar when foo is either nil? or false
a = foo || bar
In terms of "operators" I found an (unofficial) thing here for reference: Ruby operators
<=> the "spaceship" or comparison operator
=== the "trequals" or case matching operator

Resources