Hi
Why do I get an error (ruby 2.3.0)?
[1,2,3]&.upcase
What am I doing wrong here?
(I know there is no Array.upcase)
Thanks
Berg
Hi
Why do I get an error (ruby 2.3.0)?
[1,2,3]&.upcase
What am I doing wrong here?
(I know there is no Array.upcase)
Thanks
Berg
What are you trying to do? The error message seems clear enough:
irb(main):002:0> [1,2,3]&.upcase
NoMethodError: undefined method `upcase' for [1, 2, 3]:Array
from (irb):2
On Sat, Apr 2, 2016 at 5:01 PM, A Berger <aberger7890@gmail.com> wrote:
Hi
Why do I get an error (ruby 2.3.0)?
[1,2,3]&.upcase
What am I doing wrong here?
(I know there is no Array.upcase)Thanks
BergUnsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Hi
I want to understand the usage of & .
Thought this should produce no error!?
Thanks
Berg
Understand '&' under what context? Like a bit AND operator like this
irb(main):023:0> "%b" % (0b1111 & 0b1001)
=> "1001"
Focus on what's going on inside the round brackets ( ) with the 2 bit values.
The "%b" % at the start is to format the output to a binary format,
and the prefix '0b' is a way to represent a value in binary format.
On Sat, Apr 2, 2016 at 8:34 PM, A Berger <aberger7890@gmail.com> wrote:
Hi
I want to understand the usage of & .
Thought this should produce no error!?Thanks
BergUnsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
Kind Regards,
Rajinder Yadav
SafetyNet Test Driven Development
http://safetynet.devmentor.org
The "safe navigation operator" &. _only_ prevents NoMethodError
when the calling object is nil, not for any other object.
Its purpose is to avoid nil checks.
You can find examples e.g. here:
http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/
Regards,
Marcus
Am 03.04.2016 um 02:34 schrieb A Berger:
I want to understand the usage of & .
Thought this should produce no error!?
--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A
`a&.b` is like `a && a.b`
nil&.foo #=> nil
'x'&.foo #=> no method error
On 03/04/2016 10:35 AM, "A Berger" <aberger7890@gmail.com> wrote:
Hi
I want to understand the usage of & .
Thought this should produce no error!?
Thanks
Berg
almost...
false&.foo # => NoMethodError
false && false.foo # => false
Regards,
Marcus
Am 03.04.2016 um 05:24 schrieb Matthew Kerwin:
`a&.b` is like `a && a.b`
nil&.foo #=> nil
'x'&.foo #=> no method error
--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A
Indeed, which is why I didn't say it's "the same as," just "like." It also
only invokes the receiver once:
def a() puts 'a'; 'a'; end
def b() puts 'b'; 'b'; end
a && a.upcase # prints twice
b&.upcase # prints once
Cheers
On 3 April 2016 at 18:11, <sto.mar@web.de> wrote:
Am 03.04.2016 um 05:24 schrieb Matthew Kerwin:
> `a&.b` is like `a && a.b`
>
> nil&.foo #=> nil
> 'x'&.foo #=> no method erroralmost...
false&.foo # => NoMethodError
false && false.foo # => falseRegards,
Marcus
--
Matthew Kerwin
http://matthew.kerwin.net.au/
Sorry, I didn't catch that fine distinction
Am 03.04.2016 um 11:20 schrieb Matthew Kerwin:
Indeed, which is why I didn't say it's "the same as," just "like."
--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A
Hi all,
Thanks for that many very explainative answers;
much more details than could be found in the internet
- and also helpful for all (others)!
Berg