tags:

views:

38

answers:

3
    # use a YML file, which has key , value pairs
    yml_hosts = YAML::load(File.open('hosts.yml'))

      .....
     # for each pair
     yml_hosts.each_pair {|key_hosts , value_hosts|

       ......

     redirect to a String "value_hosts"

         value_hosts << "#{$.} #{line}" if line =~ /recoverable NFE/

Is there a better way of doing this, since i am using the condition:

if ! value_hosts.empty?
   to do an action, like sending email, etc
   but value_hosts is never Empty
    so i always get an email, even though, i ONLY want top get an email, if

line =~ /recoverable NFE/
+1  A: 

It depends on the class. Have a look at the docs to figure out how to use them.

Here's for String and Array classes.

Also, don't forget you can overwrite the << operators if you feel the implementation is not adequate for your purposes.

Pran
+1  A: 

It's just a method that is typically used to append data to the caller, whether it's adding a new element to an array, or appending new text to a string.

Maybe you're asking how it is appropriate to use it to conform with the "Ruby Way."

I only use it when adding an object to a collection (array).


In your code example you could replace this expression:

! value_hosts.empty?

with something like

value_hosts.mailable?

and define the rule in the mailable? method.

ryw
A: 

<< is just another method.

a << 4

is the same as

a.<<(4)

It is defined and executed and evaluated in the same way as every other method. Do whatever it is as you like.

Justin L.