setup print out in custom def

Hi i am working with a class of multiple methods

Class Bar
   Def self.Foo1 (*args)
      Do and print somethings
      return result
      end#Def
   Def self.Foo2 (*args)
      Do and print somethings
      return result
      end#Def
   Def self.Foo3 (*args)
      Do and print somethings
      return result
      end#Def

i want to set that the def Foo should print out only when i write
Bar.Foo1(*args).print_out and hide print with Bar.Foo1(*arg) only. Adding
one more argument is inconvenient for me (too much)

Thanks

Sorry, not really sure what you're trying to do -- can you post an actual
code snippet and a test that demonstrates what you want?

···

On Mon, Jan 1, 2018 at 9:08 AM, ng khanh <cnkhanh1986@gmail.com> wrote:

i want to set that the def Foo should print out only when i write
Bar.Foo1(*args).print_out and hide print with Bar.Foo1(*arg) only. Adding
one more argument is inconvenient for me (too much)

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

hi below of my def with print_out as argument. i would like to print_out as
a method for example *self.wk_obs(wk,input,index),print_out* to get it
prints out instead input as argument because i have many similar defs like
that
thanks

def self.wk_obs(wk,input,index=0,print_out = true) # one only exact
return false unless (0..4).include?index
case index
when 0
return false unless input.is_a?String
res = wk.getObjectsByName(input)
when 1
return false unless check_idd(input)
res = wk.getObjectsByType(input.to_IddObjectType)
when 2
check = input.kind_of?(Array) ? input.all? {|x| x.kind_of?(String)} : input.
kind_of?(String)
return false unless check == true
res = wk.getObjectsByReference(input)
when 3
check = input.kind_of?(Hash) ? input.size == 1 : false
return false unless check == true
return false unless check_idd(input.keys[0])
res = wk.getObjectsByTypeAndName(input.keys[0],input.values[0])
when 4
check = input.kind_of?(Hash) ? input.size == 1 : false
return false unless check == true
return false unless input.keys[0].kind_of?(String)
ref = input.values[0]
check = ref.kind_of?(Array) ? ref.all? {|x| x.kind_of?(String)} : ref.
kind_of?(String)
return false unless check == true
return false if wk.getObjectByNameAndReference(input.keys[0],[*ref]).empty?
res = wk.getObjectByNameAndReference(input.keys[0],[*ref] ).get
end
ap res.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}"} if
print_out # print_out come from argument option
return [*res]
end #def

···

On Mon, Jan 1, 2018 at 12:56 PM, Hassan Schroeder < hassan.schroeder@gmail.com> wrote:

On Mon, Jan 1, 2018 at 9:08 AM, ng khanh <cnkhanh1986@gmail.com> wrote:

> i want to set that the def Foo should print out only when i write
> Bar.Foo1(*args).print_out and hide print with Bar.Foo1(*arg) only. Adding
> one more argument is inconvenient for me (too much)

Sorry, not really sure what you're trying to do -- can you post an actual
code snippet and a test that demonstrates what you want?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

To call #print_out on the object returned from #wks_obj, you have to
make #wks_obj return an object that has a #print_out method.

Alternatively you could use #tap :

···

~~~
my_thing.wks_obj(wk, input).tap {|res| ap res.map {|x|
"#{x.nameString} : #{x.iddObject.type.valueName}" }
~~~

Cheers

On 2 January 2018 at 08:33, ng khanh <cnkhanh1986@gmail.com> wrote:

hi below of my def with print_out as argument. i would like to print_out as
a method for example self.wk_obs(wk,input,index),print_out to get it prints
out instead input as argument because i have many similar defs like that
thanks

def self.wk_obs(wk,input,index=0,print_out = true) # one only exact
return false unless (0..4).include?index
case index
when 0
return false unless input.is_a?String
res = wk.getObjectsByName(input)
when 1
return false unless check_idd(input)
res = wk.getObjectsByType(input.to_IddObjectType)
when 2
check = input.kind_of?(Array) ? input.all? {|x| x.kind_of?(String)} :
input.kind_of?(String)
return false unless check == true
res = wk.getObjectsByReference(input)
when 3
check = input.kind_of?(Hash) ? input.size == 1 : false
return false unless check == true
return false unless check_idd(input.keys[0])
res = wk.getObjectsByTypeAndName(input.keys[0],input.values[0])
when 4
check = input.kind_of?(Hash) ? input.size == 1 : false
return false unless check == true
return false unless input.keys[0].kind_of?(String)
ref = input.values[0]
check = ref.kind_of?(Array) ? ref.all? {|x| x.kind_of?(String)} :
ref.kind_of?(String)
return false unless check == true
return false if wk.getObjectByNameAndReference(input.keys[0],[*ref]).empty?
res = wk.getObjectByNameAndReference(input.keys[0],[*ref] ).get
end
ap res.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}"} if
print_out # print_out come from argument option
return [*res]
end #def

On Mon, Jan 1, 2018 at 12:56 PM, Hassan Schroeder > <hassan.schroeder@gmail.com> wrote:

On Mon, Jan 1, 2018 at 9:08 AM, ng khanh <cnkhanh1986@gmail.com> wrote:

> i want to set that the def Foo should print out only when i write
> Bar.Foo1(*args).print_out and hide print with Bar.Foo1(*arg) only.
> Adding
> one more argument is inconvenient for me (too much)

Sorry, not really sure what you're trying to do -- can you post an actual
code snippet and a test that demonstrates what you want?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

(Obviously you'd want the missing curly braces and everything, and to
make sure #ap is called in the right context, since I've never heard
of that method, etc.)

Cheers

···

On 2 January 2018 at 08:59, Matthew Kerwin <matthew@kerwin.net.au> wrote:

To call #print_out on the object returned from #wks_obj, you have to
make #wks_obj return an object that has a #print_out method.

Alternatively you could use #tap :

~~~
my_thing.wks_obj(wk, input).tap {|res| ap res.map {|x|
  "#{x.nameString} : #{x.iddObject.type.valueName}" }
~~~

Cheers

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

thanks would check it any other approach is also welcome
#ap comes from awesome_print gems (works well in VS Code and Iruby for
prettify data)

···

On Mon, Jan 1, 2018 at 5:00 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

On 2 January 2018 at 08:59, Matthew Kerwin <matthew@kerwin.net.au> wrote:
>
> To call #print_out on the object returned from #wks_obj, you have to
> make #wks_obj return an object that has a #print_out method.
>
> Alternatively you could use #tap :
>
> ~~~
> my_thing.wks_obj(wk, input).tap {|res| ap res.map {|x|
> "#{x.nameString} : #{x.iddObject.type.valueName}" }
> ~~~
>
> Cheers
>

(Obviously you'd want the missing curly braces and everything, and to
make sure #ap is called in the right context, since I've never heard
of that method, etc.)

Cheers
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi Ng Khanh,

This may not be the most performant method by it will do what you are asking:

def self.wk_obs(wk,input,index=0,print_out = true) # one only exact
    return false unless (0..4).include?index
    case index
    when 0
        return false unless input.is_a?String
        res = wk.getObjectsByName(input)
    when 1
        return false unless check_idd(input)
        res = wk.getObjectsByType(input.to_IddObjectType)
    when 2
        check = input.kind_of?(Array) ? input.all? {|x| x.kind_of?(String)} : input.kind_of?(String)
        return false unless check == true
        res = wk.getObjectsByReference(input)
    when 3
        check = input.kind_of?(Hash) ? input.size == 1 : false
        return false unless check == true
        return false unless check_idd(input.keys[0])
        res = wk.getObjectsByTypeAndName(input.keys[0],input.values[0])
    when 4
        check = input.kind_of?(Hash) ? input.size == 1 : false
        return false unless check == true
        return false unless input.keys[0].kind_of?(String)
        ref = input.values[0]
        check = ref.kind_of?(Array) ? ref.all? {|x| x.kind_of?(String)} : ref.kind_of?(String)
        return false unless check == true
        return false if wk.getObjectByNameAndReference(input.keys[0],[*ref]).empty?
        res = wk.getObjectByNameAndReference(input.keys[0],[*ref] ).get
    end
    # Object to return
    re = [*res]
    # Add the print_out method to this instance of Array
    re.define_singleton_method(:print_out){
      ap self.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}"}
    }
    # return the array
    re
end #def

call the method and it will return an array with the print_out method

Dan

@ Dan Fitzpatrick
thanks, it works well for one def.
May i move it to class in order to apply to multiple def ?
Khanh

···

On Mon, Jan 1, 2018 at 5:32 PM, Dan Fitzpatrick <dan@eparklabs.com> wrote:

Hi Ng Khanh,

This may not be the most performant method by it will do what you are
asking:

def self.wk_obs(wk,input,index=0,print_out = true) # one only exact
    return false unless (0..4).include?index
    case index
    when 0
        return false unless input.is_a?String
        res = wk.getObjectsByName(input)
    when 1
        return false unless check_idd(input)
        res = wk.getObjectsByType(input.to_IddObjectType)
    when 2
        check = input.kind_of?(Array) ? input.all? {|x|
x.kind_of?(String)} : input.kind_of?(String)
        return false unless check == true
        res = wk.getObjectsByReference(input)
    when 3
        check = input.kind_of?(Hash) ? input.size == 1 : false
        return false unless check == true
        return false unless check_idd(input.keys[0])
        res = wk.getObjectsByTypeAndName(input.keys[0],input.values[0])
    when 4
        check = input.kind_of?(Hash) ? input.size == 1 : false
        return false unless check == true
        return false unless input.keys[0].kind_of?(String)
        ref = input.values[0]
        check = ref.kind_of?(Array) ? ref.all? {|x| x.kind_of?(String)} :
ref.kind_of?(String)
        return false unless check == true
        return false if wk.getObjectByNameAndReference(
input.keys[0],[*ref]).empty?
        res = wk.getObjectByNameAndReference(input.keys[0],[*ref] ).get
    end
    # Object to return
    re = [*res]
    # Add the print_out method to this instance of Array
    re.define_singleton_method(:print_out){
      ap self.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}"}
    }
    # return the array
    re
end #def

call the method and it will return an array with the print_out method

Dan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Yes, you can create a class too:

# Create the response class extending Array
class ObsResponse < Array
  def print_out
    ap self.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}”}
  end
end

Then your method would look like this:

def self.wk_obs(wk,input,index=0,print_out = true) # one only exact
   return false unless (0..4).include?index
   case index
   when 0
       return false unless input.is_a?String
       ObsResponse.new wk.getObjectsByName(input)
   when 1
       return false unless check_idd(input)
       ObsResponse.new wk.getObjectsByType(input.to_IddObjectType)
   when 2
       check = input.kind_of?(Array) ? input.all? {|x| x.kind_of?(String)} : input.kind_of?(String)
       return false unless check == true
       ObsResponse.new wk.getObjectsByReference(input)
   when 3
       check = input.kind_of?(Hash) ? input.size == 1 : false
       return false unless check == true
       return false unless check_idd(input.keys[0])
       ObsResponse.new wk.getObjectsByTypeAndName(input.keys[0],input.values[0])
   when 4
       check = input.kind_of?(Hash) ? input.size == 1 : false
       return false unless check == true
       return false unless input.keys[0].kind_of?(String)
       ref = input.values[0]
       check = ref.kind_of?(Array) ? ref.all? {|x| x.kind_of?(String)} : ref.kind_of?(String)
       return false unless check == true
       return false if wk.getObjectByNameAndReference(input.keys[0],[*ref]).empty?
       ObsResponse.new wk.getObjectByNameAndReference(input.keys[0],[*ref] ).get
   end
end #def

You can name the ObsResponse class whatever you want.

Great thanks to all suggestions this approach matches my expectation

def setPrinout(ob)
ob.define_singleton_method(:print_out){
self.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}"}
}
end
setPrinout(re)

···

On Mon, Jan 1, 2018 at 6:49 PM, ng khanh <cnkhanh1986@gmail.com> wrote:

@ Dan Fitzpatrick
thanks, it works well for one def.
May i move it to class in order to apply to multiple def ?
Khanh

On Mon, Jan 1, 2018 at 5:32 PM, Dan Fitzpatrick <dan@eparklabs.com> wrote:

Hi Ng Khanh,

This may not be the most performant method by it will do what you are
asking:

def self.wk_obs(wk,input,index=0,print_out = true) # one only exact
    return false unless (0..4).include?index
    case index
    when 0
        return false unless input.is_a?String
        res = wk.getObjectsByName(input)
    when 1
        return false unless check_idd(input)
        res = wk.getObjectsByType(input.to_IddObjectType)
    when 2
        check = input.kind_of?(Array) ? input.all? {|x|
x.kind_of?(String)} : input.kind_of?(String)
        return false unless check == true
        res = wk.getObjectsByReference(input)
    when 3
        check = input.kind_of?(Hash) ? input.size == 1 : false
        return false unless check == true
        return false unless check_idd(input.keys[0])
        res = wk.getObjectsByTypeAndName(input.keys[0],input.values[0])
    when 4
        check = input.kind_of?(Hash) ? input.size == 1 : false
        return false unless check == true
        return false unless input.keys[0].kind_of?(String)
        ref = input.values[0]
        check = ref.kind_of?(Array) ? ref.all? {|x| x.kind_of?(String)} :
ref.kind_of?(String)
        return false unless check == true
        return false if wk.getObjectByNameAndReference
(input.keys[0],[*ref]).empty?
        res = wk.getObjectByNameAndReference(input.keys[0],[*ref] ).get
    end
    # Object to return
    re = [*res]
    # Add the print_out method to this instance of Array
    re.define_singleton_method(:print_out){
      ap self.map {|x| "#{x.nameString} : #{x.iddObject.type.valueName}"}
    }
    # return the array
    re
end #def

call the method and it will return an array with the print_out method

Dan

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;