ruby methods with a question mark ? for pattern matching? [closed] - ruby

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Ruby methods that end with a question mark at the end of the name; traditionally returns true or false.
Example:
if success?
puts "yes"
else
puts "oh nos"
end
Is there an accepted style for pattern matching that asks a question and returns [:ok] or [:error, ...]?
For example:
case authorization_valid?
in [:error, msg]
puts "error: #{msg}"
in [:ok]
puts "yes"
end
Instead of a true or false, we are using an :ok, :error.
ruby methods with a question mark ? for pattern matching?
Thoughts?
-daniel

There is no such standard. You might be interested in using Result from dry-monads:
def method_that_return_a_result
if condition
Success :foo
else
Failure :bar
end
end
case method_that_return_a_result
in Success(value)
puts value
in Failure('some specific value')
puts 'this happened, sorry'
in Failure(String => error)
raise ProcessingError, error
in Failure(other)
puts "Unexpected failure #{other.inspect}"
end
(code is just an example of what you can achieve)
Your case would read
case authorization_valid?
in Failure(msg)
puts "error: #{msg}"
in Success
puts "yes"
end

Related

Ruby <, <=, >, >= value comparison code [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
The intent is to display message based on age.
puts "Age, please"
value = gets.chomp
if value < 21
puts "Here you cannot to buy alchohol"
end
puts "You can buy all the alchohol you want"
What is the missing part in this code?
if you get value by gets, value is String.
Use value.to_i
puts "Age, please"
value = gets
value = Integer(value) rescue 0
if value < 21
puts "Here you cannot to buy alchohol"
else
puts "You can buy all the alchohol you want"
end

Ruby NameError: Undefined local variable [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 8 years ago.
Improve this question
Just started to go through the 7 languages in 7 weeks book; day one problem make a simple higher or lower game in ruby. When I execute my script I get the following error but I don't know why.
EDIT: This is thrown after I guess once.
NameError: undefined local variable or method actual' for main:Object
from guess_game.rb:2:inguess'
from guess_game.rb:22
def guess(guess, acutal)
unless guess == actual
if guess > actual
puts 'Lower'
else
puts 'Higher'
return false
end
end
puts 'Correct'
return true
end
answer = rand(10)
game_won = false
puts 'I am thinking of a number, what is it?'
until game_won
num = gets.to_i
# Static Debug Line
puts "Guess #{num} : Answer #{answer}"
game_won = guess(num, answer)
end
def guess(guess, actual)
unless guess == actual
if guess > actual
puts 'Lower'
else
puts 'Higher'
return false
end
end
puts 'Correct'
return true
end
Problem: Spelling mistake, Corrected

How to get keys from hash 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 9 years ago.
Improve this question
The next code search if keyword appear in hash values and print yes if so,
but it works well in codeacademy console, but in my Rubymine it give me exception
NoMethodError: undefined method `keys' for nil:NilClass
I've tried to use each_key method but it was the same rusult.
arr = [
{ name: "2222", num:"4444 kod"},
{ name: "3222 kod", num:"43423444"},
{ name: "224422", num:"4442424"}
]
p = "kod"
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
esle
puts "no"
end
end
Can you give some advice?)
You have 2 mistakes:
You wrote esle instead of else
You are missing one end clause
Your blocks need end keyword. And else should be spelt correctly.
arr.each do |frelancer|
frelancer.keys.each do |key|
if frelancer[key].split(" ").include? (p)
puts "yes"
else
puts "no"
end
end
end

How to use code block in 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 8 years ago.
Improve this question
I have the following code
animals=['lion','tiger','zebra']
animals.each{|a| puts a}
I wanted to print only tiger in this array for that I wrote something like this
animals.each{|a| if a==1 puts animals[a]}
But it's not working why?
You can play with enumerable like this:
animals.select{ |a| a == 'tiger' }.each{ |a| puts a }
The wrong you did in your case:-
animals.each{|a| if animals[a]==2 puts a}
inline if statement you put in a wrong way.
#each passes element of the array,not the index. So animals[a] will not work. It will throw error as no implicit conversion of String into Integer (TypeError).
Do this as below using Array#each_index
animals=['lion','tiger','zebra']
animals.each_index{|a| puts animals[a] if animals[a] == 'tiger' }
# >> tiger
Maybe you are looking for this
animals.each_with_index{|animal, index| puts animal if index==1}
Please not that "tiger" occurs at index 1 and not 2.
you can simply do this
animals.fetch(animals.index('tiger')) if animals.include? 'tiger'
or
animals[animals.index('tiger')] if animals.include? 'tiger'

ruby metaprogramming better solution [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How this will work without eval
%w{user_id for_whom_id category_id product_status_id}.each do |f|
code = <<-C
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.#{f} = nil }
end
C
eval code
end
?
This should work:
%w{user_id for_whom_id category_id product_status_id}.each do |f|
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.public_send "#{f}=", nil }
end
end
It's best to avoid the eval if possible. And in this case, it isn't necessary.

Resources