Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?
It should be trivial, but if it exists…there are better things I can do
with my time
Cheers,
Thomas
Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?
It should be trivial, but if it exists…there are better things I can do
with my time
Cheers,
Thomas
Hello –
Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?It should be trivial, but if it exists…there are better things I can do
with my time
I’m almost certainly reinventing_the_wheel, and probably doing it
badly, but I don’t mind having this hanging around on my system, so
here it is:
#!/usr/bin/ruby -wp
BEGIN {
class String
def decamel
gsub!(/\b[^\WA-Z]+[A-Z].*?\b/) do |x|
x.gsub!(/[A-Z][^\WA-Z]+/) {|s| “" + s.downcase }
x.gsub!(/[A-Z][A-Z]+/) {|s| "” + s }
x
end
end
end
}
$_.decamel
I’m assuming that class names like this should not be changed:
class SomeClass
and that in cases like this:
someDBMMethod
“DBM” should be kept uppercase.
Comments, changes, dismissal, etc. welcome
David
On Mon, 16 Sep 2002, [iso-8859-1] Thomas Søndergaard wrote:
–
David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com
Thomas Søndergaard wrote:
Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?
Forgive my ignorance. I know the difference of the two but why are you
changing it? Did the Ruby consensus decided that is the way to code? Not
a dig…I am a newbie to programming and so have no habits it this area.
I really am just wondering the why of it.
Bob
In C it’s normal to use the ruby_case, c_case …etc unlike Java and
C++ whereby one normally would use CamalCase. Since Ruby is `C’
based, this may have something to do with it’s predominant use in
Ruby.
//ed
Two comments:
#gsub! has big teeth and will bite you:
“FooBar”.decamel # ==> nil
It seems ok with #gsub instead of the first occurrence of #gsub!.
How will you handle quotation of various kinds?
“decamel replaces ‘camelVar’ with ‘camel_var’”.decamel
The fact that %{ } can be nested means you’ll need something more than
regexes…
(p.s., for some reason my mail client, Mozilla 1.0, won’t quote your
message…)
Thomas Søndergaard wrote:
Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?Forgive my ignorance. I know the difference of the two but why are you
changing it? Did the Ruby consensus decided that is the way to code? Not
a dig…I am a newbie to programming and so have no habits it this area.
I really am just wondering the why of it.
I don’t know that it’s a consensus really, and I
think calling it ruby_case is a bit extreme.
Matz does prefer underscores, I think. camelCase
is used nowhere in the core AFAIK.
The worst way, of course, is to mix the two
according to no apparent logic… as I do.
Hal
----- Original Message -----
From: “Bob X” bobx@linuxmail.org
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, September 16, 2002 4:12 PM
Subject: Re: camelCaseTo_ruby_case.rb ??
Thomas Søndergaard wrote:
Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?Forgive my ignorance. I know the difference of the two but why are you
changing it? Did the Ruby consensus decided that is the way to code? Not
a dig…I am a newbie to programming and so have no habits it this area.
I really am just wondering the why of it.
I hope it’s not a consensus yet. I’ve always used camelCase. I have actually
tried to use hyphens; even went so far as to convert whole programs to
hyphens, but my fingers simply didn’t agree with the decision. So now, names
I invent are camelCase and names from ruby objects I leave hyphened.
Actually, (a rationalization) I can now tell at a glance which are mine and
which are ruby’s.
On Monday 16 September 2002 04:12 pm, Bob X wrote:
Bob
I want my modules to be as consistent with the ruby platform as
possible, for my own sake and for others.
Sticking to your own, or your companys, coding conventions can be a
pain, now that most languages come with a substantial class library. For
instance, when I started using C# at work, we decided to stick with
java/smalltalk style method names that start with a lower-case letter,
where Microsoft use a leading upper-case letter. The problem is that you
will invariably be implementing interfaces and specializing classes
defined in the standard class library, and thus the style of the
standard library will creep into your code.
Consistency wins over habbit - at least that’s how I feel about it.
Cheers,
Thomas
On Mon, 2002-09-16 at 23:12, Bob X wrote:
Forgive my ignorance. I know the difference of the two but why are you
changing it? Did the Ruby consensus decided that is the way to code? Not
a dig…I am a newbie to programming and so have no habits it this area.
I really am just wondering the why of it.
Hi –
Two comments:
#gsub! has big teeth and will bite you:
“FooBar”.decamel # ==> nil
It seems ok with #gsub instead of the first occurrence of #gsub!.
How strange. When I do this in irb, I get nil, as you say. But when
I run my little script:
#!/usr/bin/ruby -wp
BEGIN {
class String
def decamel
gsub!(/\b[^\WA-Z]+[A-Z].*?\b/) do |x|
x.gsub!(/[A-Z][^\WA-Z]+/) {|s| “" + s.downcase }
x.gsub!(/[A-Z][A-Z]+/) {|s| "” + s }
x
end
end
end
}
$_.decamel
I get this (where odd-numbered lines are typed in and even ones are the
output):
someMethodOrOther
some_method_or_other
SomeClass
SomeClass
which is what I wanted. I can’t quite puzzle through why there would
be that difference.
How will you handle quotation of various kinds?
“decamel replaces ‘camelVar’ with ‘camel_var’”.decamel
==> “decamel replaces ‘camel_var’ with ‘camel_var’”
The fact that %{ } can be nested means you’ll need something more than
regexes…
Yes, a full-featured decameler would have to get over that hump (ha
ha). Mine is very mini and quick-n-dirty, probably to the point of
pointlessness. I imagine there’s literally no way to do it perfectly,
without harnessing an actual Ruby parser.
Actually a decameler was one of things considered for the Austin Ruby
codefest. Since we opted for scanf, the decamler project is still up
for grabs… (Or has it been done? I never did research the
original question
David
On Mon, 16 Sep 2002, Joel VanderWerf wrote:
–
David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com
Hi –
From: “Bob X” bobx@linuxmail.org
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, September 16, 2002 4:12 PM
Subject: Re: camelCaseTo_ruby_case.rb ??Thomas Søndergaard wrote:
Hi,
I’ll be converting my ruby code from camelCase to ruby_case. Is there a
script out there to get me going?Forgive my ignorance. I know the difference of the two but why are you
changing it? Did the Ruby consensus decided that is the way to code? Not
a dig…I am a newbie to programming and so have no habits it this area.
I really am just wondering the why of it.I don’t know that it’s a consensus really, and I
think calling it ruby_case is a bit extreme.
Oh, I don’t know – I was warming to that
Matz does prefer underscores, I think. camelCase
is used nowhere in the core AFAIK.The worst way, of course, is to mix the two
according to no apparent logic… as I do.
I like Maki’s guidelines at:
http://rwiki.jin.gr.jp/cgi-bin/rw-cgi.rb?cmd=view;name=RubyCodingConvention
which I think correspond also to what Matz and others have done in the
core. (I don’t agree with the noun/verb distinction for class/method,
though, but that’s not relevant to the style things.)
David
On Tue, 17 Sep 2002, Hal E. Fulton wrote:
----- Original Message -----
–
David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com
Hal E. Fulton wrote:
I don’t know that it’s a consensus really, and I
think calling it ruby_case is a bit extreme.Matz does prefer underscores, I think. camelCase
is used nowhere in the core AFAIK.The worst way, of course, is to mix the two
according to no apparent logic… as I do.Hal
Good 'nuff for me…
dblack@candle.superlink.net wrote:
…
Actually a decameler was one of things considered for the Austin Ruby
codefest. Since we opted for scanf, the decamler project is still up
for grabs… (Or has it been done? I never did research the
original question
It’s cool with me that you opted that way; scanf is nice to have around
dblack@candle.superlink.net wrote:
Hi –
Two comments:
- #gsub! has big teeth and will bite you:
“FooBar”.decamel # ==> nil
It seems ok with #gsub instead of the first occurrence of #gsub!.
How strange. When I do this in irb, I get nil, as you say. But when
I run my little script:#!/usr/bin/ruby -wp
BEGIN {
class String
def decamel
gsub!(/\b[^\WA-Z]+[A-Z].*?\b/) do |x|
x.gsub!(/[A-Z][^\WA-Z]+/) {|s| “" + s.downcase }
x.gsub!(/[A-Z][A-Z]+/) {|s| "” + s }
x
end
end
end
}$_.decamel
I get this (where odd-numbered lines are typed in and even ones are the
output):someMethodOrOther
some_method_or_other
SomeClass
SomeClasswhich is what I wanted. I can’t quite puzzle through why there would
be that difference.
I guess -p calls #print, which prints $_, and ignores the value of the
expression.
On Mon, 16 Sep 2002, Joel VanderWerf wrote:
It's a lot to ask for, but if someone wants to try and implement the
equivalent of Tcl's "clock scan" command in pure Ruby, I'd /really/ like
that.
The Tcl solution is incredibly robust and pretty powerful -- but, it's
implemented in C with a full-fledged parser using lex/yacc.
Anyhow, it's a thought ...
-- Dossy
On 2002.09.16, dblack@candle.superlink.net <dblack@candle.superlink.net> wrote:
Actually a decameler was one of things considered for the Austin Ruby
codefest. Since we opted for scanf, the decamler project is still up
for grabs.... (Or has it been done? I never did research the
original question
--
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
"He realized the fastest way to change is to laugh at your own
folly -- then you can let go and quickly move on." (p. 70)
Just an idea: you have an actual Ruby parser conveniently available, one
that can even decamel C-extensions – that Ruby parser that we know and
love:
module DeCamel
alias method_missing camel_method_missing
def method_missing (*args)
m = args.first.camel
if methods.include? m
send (m, *args[1…-1])
else
camel_method_missing (*args)
end
end
end
class CamelClass
include DeCamel
end
Ok, untested an propably doesn’t work as it is…
– Nikodemus
On Mon, 16 Sep 2002 dblack@candle.superlink.net wrote:
Yes, a full-featured decameler would have to get over that hump (ha
ha). Mine is very mini and quick-n-dirty, probably to the point of
pointlessness. I imagine there’s literally no way to do it perfectly,
without harnessing an actual Ruby parser.
I’m glad you think so. Most things that get released
don’t get many comments one way or the other.
Hal
----- Original Message -----
From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, September 15, 2002 10:13 PM
Subject: Re: camelCaseTo_ruby_case.rb ??
dblack@candle.superlink.net wrote:
…Actually a decameler was one of things considered for the Austin Ruby
codefest. Since we opted for scanf, the decamler project is still up
for grabs… (Or has it been done? I never did research the
original questionIt’s cool with me that you opted that way; scanf is nice to have around
In article 20020916033549.GD8112@panoptic.com,
Dossy dossy@panoptic.com wrote:
On 2002.09.16, dblack@candle.superlink.net dblack@candle.superlink.net wrote:
Actually a decameler was one of things considered for the Austin Ruby
codefest. Since we opted for scanf, the decamler project is still up
for grabs… (Or has it been done? I never did research the
original questionIt’s a lot to ask for, but if someone wants to try and implement the
equivalent of Tcl’s “clock scan” command in pure Ruby, I’d /really/ like
that.The Tcl solution is incredibly robust and pretty powerful – but, it’s
implemented in C with a full-fledged parser using lex/yacc.
What does Tcl’s ‘clock scan’ do?
Phil
Hal E. Fulton wrote:
…
It’s cool with me that you opted that way; scanf is nice to have around
I’m glad you think so. Most things that get released
don’t get many comments one way or the other.
I had to do some string processing in C recently (for a plug-in .so for
a clunky old app), and so had to force myself to think in terms of scanf
again. Eventually, that app may be replaced with a Ruby app, and so the
C code may turn into Ruby code. Having scanf will make life easier.
% clock format [clock scan “8 months 3 weeks 1 day 6 hours 30 seconds ago”]
Mon Dec 24 19:25:50 EST 2001
% clock format [clock scan “next year 5 months 13 days”]
Sun Feb 29 01:27:41 EST 2004
% clock format [clock scan “next tuesday 6 hours”]
Tue Sep 24 06:00:00 EDT 2002
– Dossy
On 2002.09.16, Phil Tomson ptkwt@shell1.aracnet.com wrote:
What does Tcl’s ‘clock scan’ do?
–
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)
…
Cool! Go ahead Dossy, jump on it. =) Knowing you, it’ll be done
by, oh, 6:00 tonight?
Unit tests are done already, no doubt.
— Dossy dossy@panoptic.com wrote:
On 2002.09.16, Phil Tomson ptkwt@shell1.aracnet.com wrote:
What does Tcl’s ‘clock scan’ do?
% clock format [clock scan “8 months 3 weeks 1 day 6 hours 30
seconds ago”]
Mon Dec 24 19:25:50 EST 2001
Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/
Yahoo IM: michael_s_campbell
Do you Yahoo!?
Yahoo! News - Today’s headlines
http://news.yahoo.com