Seguir

Need help with a noob Rails question 

I have a variable that I want to check if it is not nil and that the length is equal to 11.

Currently I have this:

if video_id
if video_id.length == 11
//actions
end
end

How can I make both verifications on a single if?

Sorry, for the noob question.

Need help with a noob Rails question 

@hugo you can do:
if !video_id.nil? && video_id.length == 11
...
end
(if it's in rails, you can use video_id.present? instead to check for not being nil or blank)

Need help with a noob Rails question 

@renatolond I tried something like that and was getting a "property not set length" error. Maybe it was something else I was doing wrong.

I have done this:
unless video_id.nil? || video_id.length != 11

and it's working :) but thanks

Need help with a noob Rails question 

@hugo ah, maybe there's no `.length`, try with `.size` instead :)

Need help with a noob Rails question 

@renatolond I was being silly and doing this:

if video_id.nil && video_id.length != 11

testing for nil and not 11

Thanks a lot, I never coded in Rails and am trying to fix an issue I am having on Mastodon. Youtube is not allowing me to fetch the oembed link because I am being blocked by captcha... grrrr

Need help with a noob Rails question 

@hugo If you're using a recent Ruby (at least 2.3.0, which is pretty old already), you can use the safe-navigation operator `&.` like so:

if video&.length == 11
// stuff
end

mitrev.net/ruby/2015/11/13/the

Need help with a noob Rails question 

@meqif this looks pretty clean, I like it. Thank you so much :)

Need help with a noob Rails question 

@hugo Happy to help! 😁

Inicie a sessão para participar na conversa
Mastodon (PT)

Mastodon é uma rede social baseada em protocolos abertos da web e software livre e gratuito. É descentralizado como e-mail.