Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Zerofy

0 views
Skip to first unread message

aartist

unread,
May 25, 2005, 5:24:17 PM5/25/05
to
How I can zerofy the day and month. I like to add 0 in the beginning.
Now an object cannot have 0 at the beginning unless it is a string
object in this context.


My code:
class Fixnum
def zerofy!
self < 10 ? '0' + self.to_s : self.to_s
end
end


t = Time.at(time.to_i)
day,month,year = t.to_a[3..5]
month.zerofy!

doesn't work.

Markus

unread,
May 25, 2005, 5:37:29 PM5/25/05
to
On Wed, 2005-05-25 at 14:25, aartist wrote:
> How I can zerofy the day and month. I like to add 0 in the beginning.
> Now an object cannot have 0 at the beginning unless it is a string
> object in this context.

Does Time.at(time.to_i).strftime('%d/%m/%Y') do what you want?

--MarkusQ


aartist

unread,
May 25, 2005, 5:44:46 PM5/25/05
to
Thanks that does the trick.

I still like to make zerofy working, just to understand the concepts.

Vance Heron

unread,
May 25, 2005, 6:16:55 PM5/25/05
to
Try something like
def zerofy!
sprintf("%02d", self)
end

V

Brian Schröder

unread,
May 25, 2005, 6:23:19 PM5/25/05
to
On 26/05/05, Vance Heron <he...@jpl.nasa.gov> wrote:
> Try something like
> def zerofy!
> sprintf("%02d", self)
> end
>

or
class Numeric
def pad(length)
"%0#{length}i" % self
end
end

10.pad(3) => "010"

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


Markus

unread,
May 25, 2005, 6:25:00 PM5/25/05
to
On Wed, 2005-05-25 at 14:45, aartist wrote:
> Thanks that does the trick.
>
> I still like to make zerofy working, just to understand the concepts.
>

The code you posted works, but it may not be doing what you think it
should. Specifically, Fixnum#zerofy! returns the string you want, but
it doesn't cause the recipient to _become_ that string (since the
recipient is a Fixnum, this would involve heavy Juju and is not what
you'd what, even if you think it is). You may have mislead yourself by
sticking a bang! on the end.

--MarkusQ


Charles Steinman

unread,
May 25, 2005, 6:28:54 PM5/25/05
to
aartist wrote:
> I still like to make zerofy working, just to understand the concepts.
That method works for me.

irb(main):002:0> class Fixnum
irb(main):003:1> def zerofy!
irb(main):004:2> self < 10 ? '0' + self.to_s : self.to_s
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> 5.zerofy!
=> "05"

So I don't think your problem was where you think it is. By the way,
just as a point of style, since this isn't changing the Fixnum or doing
anything out the the ordinary really, I don't think the method ought to
have a bang on the end.

aartist

unread,
May 25, 2005, 9:08:58 PM5/25/05
to
Markus, , you are right on the point.

month = 5
month.zerofy!
puts month.class =>String
puts month => 05

Is it not possible?
I am newbie in Ruby, but I am interested in that heavy juju, you
mentioned.

ri Fixnum : gives me two important thing.
A. Fixnum objects have immediate value
B. You cannot add a singleton method.

I would like to know, if you can add destructive method to Fixnum
objects..

By the way, This is nice group, and I really like the cooperation.
Thanks for keeping up guys.

Eric Hodel

unread,
May 25, 2005, 9:27:36 PM5/25/05
to

On 25 May 2005, at 18:10, aartist wrote:

> Markus, , you are right on the point.
>
> month = 5
> month.zerofy!
> puts month.class =>String
> puts month => 05
>
> Is it not possible?

No.

> I am newbie in Ruby, but I am interested in that heavy juju, you
> mentioned.

The juju is too heavy. Bulk up on some lighter juju first. (Using
the juju when you are a newbie implies that you are probably trying
to do something you're not supposed to do.)

> ri Fixnum : gives me two important thing.
> A. Fixnum objects have immediate value
> B. You cannot add a singleton method.
>
> I would like to know, if you can add destructive method to Fixnum
> objects..

Since Fixnum objects are immediate, you can't make any changes to them.

--
Eric Hodel - drb...@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Tyler Eaves

unread,
May 26, 2005, 12:16:10 AM5/26/05
to
On Wed, 25 May 2005 14:24:17 -0700, aartist wrote:

> How I can zerofy the day and month. I like to add 0 in the beginning.
> Now an object cannot have 0 at the beginning unless it is a string
> object in this context.

I think the real problem here is that maybe you aren't quite comfortable
with the typing system. Fixnums are designed for mathematical
manipulation. Strings (in this context) are generally used for display. So
basically you want to hold off on converting to a string for as long as
possible. Generally, there's no need to do an explicit conversion. You'll
generally just use printf or something when you need to display it, but
you'll keep the integer as the actual object.

irb(main):001:0> a = 3
=> 3
irb(main):002:0> printf("%02d\n",a)
03
=> nil

Charles Steinman

unread,
May 26, 2005, 10:27:15 AM5/26/05
to
aartist wrote:
> Markus, , you are right on the point.
>
> month = 5
> month.zerofy!
> puts month.class =>String
> puts month => 05
>
> Is it not possible?
> I am newbie in Ruby, but I am interested in that heavy juju, you
> mentioned.
>
> ri Fixnum : gives me two important thing.
> A. Fixnum objects have immediate value
> B. You cannot add a singleton method.
>
> I would like to know, if you can add destructive method to Fixnum
> objects..

You really don't want to do that. You would be changing the meaning of
the number 5 for every operation afterward. You'd see cases like this:

(begin mock code)
month = 5 #=> 5
month.zerofy! #=> "05"
Timmy.age = 5 #=> "05"
#It's Timmy's birthday!
Timmy.age += 1 #ERROR! You can't add 1 to a String, which is what
zerofy! turned 5 into
(end mock code)

Keep in mind that methods do not operate on the variables you use to
call them. Variables are just references to objects, any several can
refer to the same object. Every call to the number 5 goes to the same
Fixnum object. Besides it being incredibly evil (in the Evil Ruby
sense) to transform an object into one of a completely different type,
doing so to the number 5 is a particularly diabolical concept.

What you want to do is just this, I think:

month = month.zerofy

Jim Weirich

unread,
May 26, 2005, 10:43:39 AM5/26/05
to

Charles Steinman said:
> Keep in mind that methods do not operate on the variables you use to
> call them. Variables are just references to objects, any several can
> refer to the same object.

I like to say it like this: Languages like Java and C++ encourage you to
think of variables as boxes into which you put values. In Ruby, variables
are postit notes (with names) that you stick on objects.

--
-- Jim Weirich j...@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

Christian Neukirchen

unread,
May 29, 2005, 5:22:29 AM5/29/05
to
Brian Schröder <ruby....@gmail.com> writes:

> On 26/05/05, Vance Heron <he...@jpl.nasa.gov> wrote:
>> Try something like
>> def zerofy!
>> sprintf("%02d", self)
>> end
>>
>
> or
> class Numeric
> def pad(length)
> "%0#{length}i" % self
> end
> end
>
> 10.pad(3) => "010"

or, even cleaner:

irb(main):002:0> "%0*d" % [3, 10]
=> "010"

> regards,
>
> Brian

--
Christian Neukirchen <chneuk...@gmail.com> http://chneukirchen.org


Brian Schröder

unread,
May 29, 2005, 3:24:21 PM5/29/05
to
On 29/05/05, Christian Neukirchen <chneuk...@gmail.com> wrote:
> Brian Schröder <ruby....@gmail.com> writes:
>
> > On 26/05/05, Vance Heron <he...@jpl.nasa.gov> wrote:
> >> Try something like
> >> def zerofy!
> >> sprintf("%02d", self)
> >> end
> >>
> >
> > or
> > class Numeric
> > def pad(length)
> > "%0#{length}i" % self
> > end
> > end
> >
> > 10.pad(3) => "010"
>
> or, even cleaner:
>
> irb(main):002:0> "%0*d" % [3, 10]
> => "010"
>
> > regards,
> >
> > Brian
>
Thanks,


Ah, I didn't know this.

0 new messages