Can someone please have a look at this code and tell me what I'm doing wrong?

Hi everyone,

I'm practising my ruby coding and am trying to accomplish the following?-

I want this scenario:-

6.digit? => true or "cat".digit? => false

Pass a number/string to the method digit? and it should return true or
false.

This is my code:-

def digit?
  \^/d*$\ =~ self ? "true" : "false"
end

When I do this, I get 6.digit? => false, 9.digit? => false

Can someone please have a look at my code and tell me what I'm doing wrong?

Many thanks

You have a number of errors. I'll step you through each of them:

\^/d*$\ =~ self ? "true" : "false"

First, the regex. You've got your slashes and backslashes mixed up;
slashes are the delimeters to start and end a regex, backslashes for
your "\d" decimal:

/^\d*$/

However, that's still not right. Because you have '\d*' -- that says
"none or more". For your test to be correct, you need "one or more":
'\d+'

/^\d+$/ =~ self ? "true" : "false"

However, that's still not right... because you got the order of your
comparison backward. It should be variable =~ regex. So...

self =~ /^\d+$/ ? "true" : "false"

However, that's *STILL* not right, because regex works against strings,
not numbers, so, lastly,

self.to_s =~ /^\d+$/ ? "true" : "false"

*THAT*, I believe, will do what you're looking for.

-Ken

···

On 2016-01-29 09:59, angela ebirim wrote:

Hi everyone,

I'm practising my ruby coding and am trying to accomplish the following?-

I want this scenario:-

6.digit? => true or "cat".digit? => false

Pass a number/string to the method digit? and it should return true or false.

This is my code:-

def digit?
\^/d*$\ =~ self ? "true" : "false"
end

When I do this, I get 6.digit? => false, 9.digit? => false

Can someone please have a look at my code and tell me what I'm doing wrong?

Many thanks

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

Links:
------
[1] http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk

def digit?
  \^/d*$\ =~ self ? "true" : "false"
end

...

Can someone please have a look at my code and tell me what I'm doing wrong?

As Nemo said, you'll be returning the string versions of true and
false, rather than raw literal true and false, because of the quotes.
To understand his second correction, though, you need to realize that
in order to use special "escape code" chars like "d" to denote a
digit, the preceding char must be a *backslash*, not a normal slash.
If you want to use his solution, though, you'll need to remove the
spaces he put inside it. (Yeah, regex syntax is abstruse.)

Second, regexes work on strings. If you call it on, for instance, 6
(as opposed to "6"), you're going to get "TypeError: no implicit
conversion of Fixnum into String". So, you need to convert the
argument to a String if it isn't one already.

A couple other suggestions though:

- Think about what happens if you feed it something unusual, like
empty string, or maybe even nil. Try it. Is that what you wanted to
have happen? If not, how can you fix it?

- If you weren't writing it, but saw that there was a method called
"digit?", and you didn't see the code, what expectations would that
set in your mind, about what sort of test its applying? Does that
match what yours does? If not, is there maybe a better name for
yours? Good naming will help people understand your code, though it
can be difficult. (There's an old saying that there only are two
really hard things in computer science: cache invalidation, and naming
things. BTW, this has given rise to the joke that there only are two
really hard things in computer science: cache invalidation, naming
things, and off-by-one errors.)

···

On Fri, Jan 29, 2016 at 9:59 AM, angela ebirim <cebirim@gmail.com> wrote:

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

Hi,
You could try changing your regex like so:
def digit?
\^ \d+ $\ =~ self ? true : false
end
Also , true and false need not be strings.
Explanation of regex: The regex checks whether the given entity has got atleast number or not. You could also write this as:
\^ [0-9]+ $\

···

On 29-Jan-2016, at 8:29 PM, angela ebirim <cebirim@gmail.com> wrote:

Hi everyone,

I'm practising my ruby coding and am trying to accomplish the following?-

I want this scenario:-

6.digit? => true or "cat".digit? => false

Pass a number/string to the method digit? and it should return true or false.

This is my code:-

def digit?
  \^/d*$\ =~ self ? "true" : "false"
end

When I do this, I get 6.digit? => false, 9.digit? => false

Can someone please have a look at my code and tell me what I'm doing wrong?

Many thanks

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

Regex is a good way to do this problem, check out http://rubular.com/ to
play around with the rules and see whats actually being selected, I think
this will help you a lot. Another way to do this problem is to use the
#class method. Try running 10.class, "10".class, nil.class, true.class,
false.class in irb and see what happens. What about "10".class == String
This might be another way to solve this problem.

···

On Fri, Jan 29, 2016 at 7:31 AM, Amadeus Folego <amadeusfolego@gmail.com> wrote:

Also, please replace ^ with \A and $ with \z to avoid multiline matching
on the regex, I guess it's the case that you want to match only when
there's one line on the target string.

You can test your regex here: http://rubular.com/

On Fri, Jan 29, 2016 at 08:34:21PM +0530, Nemo Nautilius wrote:
> Hi,
> You could try changing your regex like so:
> def digit?
> \^ \d+ $\ =~ self ? true : false
> end
> Also , true and false need not be strings.
> Explanation of regex: The regex checks whether the given entity has got
atleast number or not. You could also write this as:
> \^ [0-9]+ $\
> > On 29-Jan-2016, at 8:29 PM, angela ebirim <cebirim@gmail.com> wrote:
> >
> > Hi everyone,
> >
> > I'm practising my ruby coding and am trying to accomplish the
following?-
> >
> > I want this scenario:-
> >
> > 6.digit? => true or "cat".digit? => false
> >
> > Pass a number/string to the method digit? and it should return true or
false.
> >
> > This is my code:-
> >
> > def digit?
> > \^/d*$\ =~ self ? "true" : "false"
> > end
> >
> > When I do this, I get 6.digit? => false, 9.digit? => false
> >
> > Can someone please have a look at my code and tell me what I'm doing
wrong?
> >
> > Many thanks
> >
> >
> > 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;

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

Oooh, good catch!

-Dave

···

On Fri, Jan 29, 2016 at 10:31 AM, Amadeus Folego <amadeusfolego@gmail.com> wrote:

Also, please replace ^ with \A and $ with \z to avoid multiline matching

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

Also, please replace ^ with \A and $ with \z to avoid multiline matching
on the regex, I guess it's the case that you want to match only when
there's one line on the target string.

You can test your regex here: http://rubular.com/

···

On Fri, Jan 29, 2016 at 08:34:21PM +0530, Nemo Nautilius wrote:

Hi,
You could try changing your regex like so:
def digit?
\^ \d+ $\ =~ self ? true : false
end
Also , true and false need not be strings.
Explanation of regex: The regex checks whether the given entity has got atleast number or not. You could also write this as:
\^ [0-9]+ $\
> On 29-Jan-2016, at 8:29 PM, angela ebirim <cebirim@gmail.com> wrote:
>
> Hi everyone,
>
> I'm practising my ruby coding and am trying to accomplish the following?-
>
> I want this scenario:-
>
> 6.digit? => true or "cat".digit? => false
>
> Pass a number/string to the method digit? and it should return true or false.
>
> This is my code:-
>
> def digit?
> \^/d*$\ =~ self ? "true" : "false"
> end
>
> When I do this, I get 6.digit? => false, 9.digit? => false
>
> Can someone please have a look at my code and tell me what I'm doing wrong?
>
> Many thanks
>
>
> 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;

Regex is a good way to do this problem,

Note, that Angela wants to do the test on different types. Here's
another solution:

irb(main):001:0> class Object; def digit?; false end end
=> :digit?
irb(main):002:0> class Integer; def digit?; (0..9).include? self; end end
=> :digit?
irb(main):003:0> 6.digit?
=> true
irb(main):004:0> "cat".digit?
=> false
irb(main):005:0> for i in -10..20; printf "%3d %p\n", i, i.digit? end
-10 false
-9 false
-8 false
-7 false
-6 false
-5 false
-4 false
-3 false
-2 false
-1 false
  0 true
  1 true
  2 true
  3 true
  4 true
  5 true
  6 true
  7 true
  8 true
  9 true
10 false
11 false
12 false
13 false
14 false
15 false
16 false
17 false
18 false
19 false
20 false
=> -10..20

check out http://rubular.com/ to
play around with the rules and see whats actually being selected, I think
this will help you a lot. Another way to do this problem is to use the
#class method. Try running 10.class, "10".class, nil.class, true.class,
false.class in irb and see what happens. What about "10".class == String
This might be another way to solve this problem.

If you want to make the result dependent on the type an approach like
the one above is preferable as it actually takes advantage of Ruby's
type system.

Kind regards

robert

···

On Fri, Jan 29, 2016 at 4:44 PM, Jay Gabriels <jgabriels30@gmail.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Yes, I agree with Dave. I should've included the to_s method. Angela you
could try writing a generic function for different types as:

def digit?(arg)
\A\d+z\ =~ arg.to_s ? true : false
end

This function will take an argument and then test whether it's a number or
not. In this way you can test this function for many types. You can also
raise an exception if a given argument can't be converted to a string and
exit the function with a simple message.

'\Az\' is in accordance with what Amadeus noted and Robert, please present
your format in a code format so that it's easier for us to understand.

Thanks.

···

On Fri, Jan 29, 2016 at 10:06 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Fri, Jan 29, 2016 at 4:44 PM, Jay Gabriels <jgabriels30@gmail.com> > wrote:
> Regex is a good way to do this problem,

Note, that Angela wants to do the test on different types. Here's
another solution:

irb(main):001:0> class Object; def digit?; false end end
=> :digit?
irb(main):002:0> class Integer; def digit?; (0..9).include? self; end end
=> :digit?
irb(main):003:0> 6.digit?
=> true
irb(main):004:0> "cat".digit?
=> false
irb(main):005:0> for i in -10..20; printf "%3d %p\n", i, i.digit? end
-10 false
-9 false
-8 false
-7 false
-6 false
-5 false
-4 false
-3 false
-2 false
-1 false
  0 true
  1 true
  2 true
  3 true
  4 true
  5 true
  6 true
  7 true
  8 true
  9 true
10 false
11 false
12 false
13 false
14 false
15 false
16 false
17 false
18 false
19 false
20 false
=> -10..20

>check out http://rubular.com/ to
> play around with the rules and see whats actually being selected, I think
> this will help you a lot. Another way to do this problem is to use the
> #class method. Try running 10.class, "10".class, nil.class, true.class,
> false.class in irb and see what happens. What about "10".class == String
> This might be another way to solve this problem.

If you want to make the result dependent on the type an approach like
the one above is preferable as it actually takes advantage of Ruby's
type system.

Kind regards

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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

You mean like that?

class Object
  def digit?; false end
end

class Integer
  def digit?; (0..9).include? self; end
end

Cheers

robert

···

On Fri, Jan 29, 2016 at 8:11 PM, Nemo Nautilius <nemodevops@gmail.com> wrote:

'\Az\' is in accordance with what Amadeus noted and Robert, please present
your format in a code format so that it's easier for us to understand.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/