Execution of ruby OR condition on an array [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
In irb
[[]] | [3]
produces
[[], 3]
I am having some difficulty understanding it. How is the OR operator working here ?

According to the documentation,
Set Union — Returns a new array by joining ary with other_ary,
excluding any duplicates and preserving the order from the original
array.
[1,2,3] | [4,5,6] # => [1, 2, 3, 4, 5, 6]
[1,2,3] | [4,1,2] # => [1, 2, 3, 4]

|| is the logical OR operator you might be thinking of.
| with arrays performs a set union operation on the arrays and gives you an array that has all the unique elements of both arrays. More details at ruby-doc

Related

Indexes in strings [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 6 years ago.
Improve this question
I have:
str = "something"
str[0, 1] #=> "s"
str[0, 2] #=> "so"
str[3, 4] #=> "ethi"
str[2, 3] #=> "meth"
I can't see logic in this. What is returned by this double indexes?
str[2,3] is "met". In this form it is str[zero_based_start_position, number_of_characters]
See ruby documentation on String class

How do you delete a key-value pair from a hash in ruby? I get an error when trying? [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 7 years ago.
Improve this question
myhash = {answer: "yes", something: hello, another: "yes"}
myhash.delete[another]
I want to delete the another key-value pair. But, ruby gives me an error saying wrong number of arguments (0 for 1). What's going on?
the method delete is a method, not an element on the hash , and another key is a symbol, so you should call on this form
myhash.delete(:another)

map hash keys in Ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have a hash
{"one"=>1, "two"=>2, "three"=>3}
What is the best way to map the keys to get the array:
["one", "two", "three"]
{"one"=>1, "two"=>2, "three"=>3}.keys #=> ["one", "two", "three"]
The documentation is awesome for discovering answers to questions like these.
If the question is "how do I get X data from object of type Y?", reading through the docs for object type Y should be the first place you go.
{"one"=>1, "two"=>2, "three"=>3}.keys

Check if array contains element and give output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
tickets = Array.new(5) {rand(10)+1}
How can I make a ruby code, that checks if any tickets equals, for example 5. I want output something like
=> "Ticket #5 wins!"
So - if tickets generate 1, 5, 4, 2, 3 and my rule is it to equal 5, it outputs
=> "Ticket #5 wins!"
To check if an array includes an element and print the result.
winner = 5
puts "Ticket ##{winner} wins!" if tickets.include? winner

Where can I find a Prime class documentation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
If i use Prime class like this:
Prime.new
I'll get next message:
Prime::new is obsolete. use Prime::instance or class methods of Prime.
I tryied finding this class documentation, but couldn't.
Unfortunately it seems not to be published yet at ruby-doc.org. But you could have a look at the commented source code for now; it includes usage examples.
You should have it locally through ri:
$ ri Prime
= Prime < Object
------------------------------------------------------------------------------
= Includes:
Enumerable (from ruby core)
(from ruby core)
------------------------------------------------------------------------------
The set of all prime numbers.
== Example
Prime.each(100) do |prime|
p prime #=> 2, 3, 5, 7, 11, ...., 97
end
== Retrieving the instance
Prime.new is obsolete. Now Prime has the default instance and you can access
it as Prime.instance.
...
There's also RubyDoc.info which has a better index:
http://rubydoc.info/stdlib/prime/1.9.2/frames

Resources