Can Ruby return nothing?
Basically, what you are looking for is a statement. But Ruby doesn't have statements, only expressions. Everything is an expression, i.e. everything returns a value.
Should Ruby method return nil or empty object
Not only in ruby but also in other languages, you should return an empty enumerable/collection. There is no need for your users to check if the return value is nil or not before doing any action, it's a waste of time.
Ruby on Rails method returning nothing
A few nits here:
You've defined a new method, but you haven't invoked it. You're likely using something like IRB to interactively play around, but the fact remains that you've only defined that method.
You're going to confuse yourself if you use
word
andnumber
as variables inside of that method, since they're not guaranteed to be the same as the ones you've defined outside of it.
If you want to pass two values to the method, then you have to specify two parameters:
def print_x_times(word, number)
# code
end
...then, you actually go about calling it with your variables.
print_x_times(word, number)
Idiomatic way of returning nil in Ruby
I try to create methods that bail out early if conditions aren't met, flattening the code in terms of indentation:
def find_something data
return unless data.has_what_we_need?
a = data.something
return unless a.has_what_we_want?
a.the_stuff[42]
end
This has two well defined exit points, as return
should show up as a fairly obvious thing when reading it. A return
with no argument will return nil
to the caller.
If you don't care about nil
vs. false
you can condense this even more:
def find_something data
return unless data.has_what_we_need?
a = data.something
a.has_what_we_want? and a.the_stuff[42]
end
Ransack: return nothing when form submitted and no search values entered
What you can do is reject any values that are blank.
if params[:q].reject { |_, v| v.blank? }.any? # .none? for inverted
# Handle search
else
# Handle no query scenario
end
Or
if params[:q].values.reject(&:blank?).any? # .none? for inverted
# Handle search
else
# Handle no query scenario
end
Request returns nothing
conn.execute(sql)
does not accept a block, it simply returns a result. The proc afterwards is treated by a ruby interpreter as “orphan proc definition” and never gets executed. You might try to put puts 'I am here'
inside it and see it is never called.
The solution would be to iterate the result:
get '/all_users/' do
conn = TinyTds::Client.new(...)
sql = 'select * from dbo.ServerUsers'
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ iterate!!!
records = conn.execute(sql).each_with_object([]) do |record, memo|
client = AndroidtableClientsSearch.new(*5.times.map { |i| record[i] })
memo << client.to_s
end
require 'json'
JSON.dump(clientList: records)
end
Return empty array (Ruby)
To fix your code, change what you have to one of the following:
num.nil? || num.empty? ? [] : num.sort
num.nil? ? [] : num.sort
(num || []).sort
num.to_a.sort
The latter two convert num
to an empty array if num
is nil
, then sort the result. See NilClass.to_a.
Related Topics
Ruby - Platform Independent Way to Determine Ips of All Network Interfaces
How to Associate a CSS/Sass Stylesheet to a View in Rails
How to Best Create a Random Float in a Range Between Two Floats
Ruby on Rails - Generating Bit.Ly Style Identifiers
Ruby Block and Returning Something from Block
Ruby String Strip Defined Characters
Static Site Generator Based Upon Directories & Files
Ruby Syntax: Break Out from 'Each.. Do..' Block
Failing to Bundle Install Tiny_Tds on MAC Os X 10.8 with Homebrew Freetds
Why Array.Reverse_Each Is Faster Than Array.Reverse.Each
Rails Guides Offline Documentation
Fresh Install of Rvm in Ubuntu Isn't Letting Me Install Gems (Zlib Error)
Ssl Error on Http Post (Unknown Protocol)
Superclass Mismatch, Struct, Reloading and Spork