Check for nil and blank

29 views
Skip to first unread message

Manthan Gandhi

unread,
Jun 22, 2011, 7:43:39 PM6/22/11
to Ruleby
Dear all,

I have a condition where I am validating String Objects. Checking
"nil" is easy. By doing

rule [m.variable==nil]
puts "Variable is nil"

But I want to check for "blank"

variable=" " (P.S. the string is not nil, it has spaces in
between)

How to check whether the string is blank?
What I am doing at present is calling a function and then checking
whether the variable is blank. But it defeats the purpose of the rule
engine.

I am doing it like this:

rule []
check_variable_is_blank(variable)

def check_variable_is_blank(variable)
if variable.blank?
puts "Variable is nil"
end
end

Thanks
MG

Joe Kutner

unread,
Jun 22, 2011, 8:08:26 PM6/22/11
to Ruleby
How about this:

rule [f(c{|m| m.empty?})]

The "f" method is new in 0.8. It allows you to pass in a Proc or
lambda and evaluate it against the fact in the pattern. The "c" is
just a shorthand we provide for lambda. Take a look at the spec/
function_spec.rb for more examples. After looking it this way, I
should problem try to provide a way to do this: f{|a| ...} if you
don't have any other args to the block.

Let me know if thats not quite what you need.

Joe

MG

unread,
Jun 22, 2011, 8:21:31 PM6/22/11
to Ruleby
Hey Joe,

Thanks for the prompt reply. "empty" doesn't work in my case. I need
"blank"
eg: In Ruby

a=" "
a.empty? gives false
a.blank? gives true

Also I am using 0.7 version. Is there any way to check this in 0.7
version?
I had one more doubt. Can we use ruby inbuilt function such
as .size, .length etc in rules.
eg.

rule[m.title.length>10]
puts "Title is too long"

Thanks
MG

Joe Kutner

unread,
Jun 22, 2011, 9:10:23 PM6/22/11
to rul...@googlegroups.com
You should be able to replace empty? in my previous example with blank? without any problems.

You cannot chain methods like you show with length. But you can do this:

rule [f(c{|m| m.title.length > 10})]

Ultimately, the forms such as [m.foo == 'bar'] are just a short hand for the function style shown above. So it would be possible to extend the DSL to allows this though.

You will have to upgrade to 0.8 to take advantage of all this.

> --
> You received this message because you are subscribed to the Google Groups "Ruleby" group.
> To post to this group, send email to rul...@googlegroups.com.
> To unsubscribe from this group, send email to ruleby+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ruleby?hl=en.
>

MG

unread,
Jun 23, 2011, 11:59:49 AM6/23/11
to Ruleby
Hey Joe,

I downloaded 0.8 version. I tried this:

class Message
def initialize(status,message)
@status = status
@message = message
end
attr :status, true
attr :message, true
end

class HelloWorldRulebook < Rulebook
def rules
rule [Message, :m, f(c{|m| m.message.blank?})] do |v|
puts "Hi"
end
end
end

engine :engine do |e|
HelloWorldRulebook.new(e).rules
e.assert Message.new(:HELLO, ' ') #NOTE- Message is NOT
BLANK. It has spaces
e.match
end


I ran this, but it doesn't print "Hi". I cannot us 'blank?' but I am
able to use 'empty?'. But empty doesn't serve my purpose. I want
'blank?'
Sorry for bugging you with so many questions

Thanks
MG

Joe Kutner

unread,
Jun 23, 2011, 12:09:35 PM6/23/11
to rul...@googlegroups.com
blank? does not belong to Ruby - it is a Rails extension.  So in order to use it you will have to require activesupport I believe.

Joe

MG

unread,
Jun 23, 2011, 12:15:08 PM6/23/11
to Ruleby
So if I want to use Ruleby in a Ruby on Rails project, then I would be
able to use 'blank?', correct?
Thanks a lot Joe for all your help!!! Appreciate it

On Jun 23, 9:09 am, Joe Kutner <jpkut...@gmail.com> wrote:
> blank? does not belong to Ruby - it is a Rails extension.  So in order to use it you will have to require activesupport I believe.http://api.rubyonrails.org/classes/Object.html#M000011
Reply all
Reply to author
Forward
0 new messages