Can someone see what I cannot given this code?
class ForexCASource
def initialize(source=nil)
xchg_source unless source
puts "this is source = #{source} #{source.class}"
puts "do we get here? why?"
xchg_source(source)
end
>> fx = ForexCASource.new
this is source = NilClass
do we get here? why?
TypeError: can''t convert nil into String
from /usr/lib/ruby/1.8/open-uri.rb:32:in
`open_uri_original_open''
from /usr/lib/ruby/1.8/open-uri.rb:32:in `open''
from
/home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:58:in
`xchg_source''
from
/home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:52:in
`initialize''
from (irb):1:in `new''
from (irb):1>>
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
James Byrne wrote:> Can someone see what I cannot given this code? > > class ForexCASource > > def initialize(source=nil) > xchg_source unless source > puts "this is source = #{source} #{source.class}" > puts "do we get here? why?" > xchg_source(source) > end > > >>> fx = ForexCASource.new > this is source = NilClass > do we get here? why? > TypeError: can''t convert nil into String > from /usr/lib/ruby/1.8/open-uri.rb:32:in > `open_uri_original_open'' > from /usr/lib/ruby/1.8/open-uri.rb:32:in `open'' > from > /home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:58:in > `xchg_source'' > from > /home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:52:in > `initialize'' > from (irb):1:in `new'' > from (irb):1xchg_source unless source This is a one-liner that just doesn''t call xchg_source if source is nil (which it is in this case). All other lines will execute normally so: xchg_source(source) will call xchg_source with a nil value. Since you haven''t posted the code for xchg_source it''s hard to tell what''s happening in there. did you mean: if !source puts "this is source = #{source} #{source.class}" puts "do we get here? why?" xchg_source(source) end Cheers, Gary, --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
James Byrne wrote:> Can someone see what I cannot given this code?I need a return in front of the statement. def initialize(source=nil) return xchg_source unless source xchg_source(source) end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
James Byrne wrote:> James Byrne wrote: >> Can someone see what I cannot given this code? > > I need a return in front of the statement. > > def initialize(source=nil) > return xchg_source unless source > xchg_source(source) > endWhy not just let xchg_source deal with the nil then you just have: def initialize(source=nil) xchg_source(source) end protected # assuming this is not part of the public interface def xchg_source(source) if source # whatever you do if source is given else # whatever should happen if source is nil end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robert Walker wrote:> Why not just let xchg_source deal with the nil then you just have: > > def initialize(source=nil) > xchg_source(source) > end > > protected # assuming this is not part of the public interfaceAh, but it is public and if I pass a nil object to it then it fails, but it does not fail if I pass it nothing at all, or if I pass it a string, or a file object, etc. Odd, but there it is. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
What about:
def initialize(source=nil)
source ? xchg_source(source) : xchg_source
end
Or if you prefer longer notation
def initialize(source=nil)
if source
xchg_source(source)
else
xchg_source
end
Or like in your example:
def initialize(source=nil)
return xchg_source unless source
xchg_source(source)
end
On Apr 17, 9:47 pm, James Byrne
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Can someone see what I cannot given this code?
>
> class ForexCASource
>
> def initialize(source=nil)
> xchg_source unless source
> puts "this is source = #{source} #{source.class}"
> puts "do we get here? why?"
> xchg_source(source)
> end
>
> >> fx = ForexCASource.new
>
> this is source = NilClass
> do we get here? why?
> TypeError: can''t convert nil into String
> from /usr/lib/ruby/1.8/open-uri.rb:32:in
> `open_uri_original_open''
> from /usr/lib/ruby/1.8/open-uri.rb:32:in `open''
> from
>
/home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:58:in
> `xchg_source''
> from
>
/home/byrnejb/Software/Development/Projects/proforma.git/app/services/forex_ca_source.rb:52:in
> `initialize''
> from (irb):1:in `new''
> from (irb):1
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---