1

I have create new array jobs and j.id is start from 423 and loop creates array index from 0 to total job ids with null value from 0 to 422 id. So my question is how to set condition to check the null value of j.name?

@jobs = []
demo.demojobs.each do | j |
    if j.name != null #condition 
        @jobs[j.id] = j.name
    end
end 

I am working on rails version 3.2.11

Output:

pipe
0: null
1: null
.
.
.
.
423:    "jobname"
424:    "jobname"
425:    "jobname"
426:    "jobname"
427:    "jobname"
5
  • From reading comments you want output in an array or hash ? Commented Jan 23, 2019 at 12:54
  • @ts I want output in array Commented Jan 23, 2019 at 12:59
  • Can you please format your array as 423 "jobname" is weird array it can be ['423 jobname', 424 jobname] Commented Jan 23, 2019 at 13:01
  • If your output array is like ['423 jobname', '424 jobname'] check my updated answer hope it helps Commented Jan 23, 2019 at 13:06
  • What is the actual input here? Is this data that you are fetching from a database query? Commented Jan 23, 2019 at 15:32

4 Answers 4

2

nil value is interpreted as false in conditions, so you can write:

@jobs = []
demo.demojobs.each do | j |
    if j.name 
        @jobs[j.id] = j.name
    end
end 

or in a more concise manner:

@jobs = []
demo.demojobs.each do | j |         
  @jobs[j.id] = j.name if j.name
end 

You can also use #nil? method if you want to check explicitly:

@jobs = []
demo.demojobs.each do | j |
    if !j.nil? 
        @jobs[j.id] = j.name
    end
end 

There is a nice blogpost explaining various options: https://blog.arkency.com/2017/07/nil-empty-blank-ruby-rails-difference/

Sign up to request clarification or add additional context in comments.

4 Comments

I have try all conditions but still same issue array start from 0 index.
I'm not sure if I understand that problem with 0 index, could you tell more about it?
I have j.id which is start from 423 so i want array create from 423 index not from 0 index.
What is your output now with answers you have tried ?
1

it's nil in Ruby, not null You can check if an object is nil by calling present? or blank?.

j.name.present?

this will return false if the name is an empty string or nil .

or you can use

j.name.blank?

Comments

1

how to set condition to check the null value of j.name?

There are many ways to check for null/nil or an empty string in ruby

  1. present? This will return true or false

    j.name.present?
    
  2. blank? This is opposite of present?

    j.name.blank?
    
  3. j.name == nil

  4. empty? can be used on strings, arrays and hashes.

Edit:

@jobs = {}
#in your loop
@jobs[j.id] = j.name if j.name #you can use any condition here to check nil

2 Comments

423, 424 are index of array not a value with jobname i need output like 423: jobname.
array index can not be start from 423 if you want like this you can use hash instead of array @jobs = {423: 'jobname', 424: 'jobname'} like this
0

You can use present? or blank? in condition. Your code should be

if j.name.present? #condition 
   @jobs[j.id] = j.name
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.