Can Ruby Return Nothing

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 and number 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



Leave a reply



Submit