return number of spaces at the beginning of a line

T

thunk

You're thinking about it the wrong way. A symbol is a pointer to a
specific address in memory, kinda like a variable.
Do you expect this code to work?

a = 5
b = NeuralNetwork.new
puts (a + b).to_s

.. You'll get an error, because a and b are just pointers to the
objects. Symbols are a bit different; while they are "faster" than plain
variables, or than strings, they never get garbage collected (I can not
explain any more than this, but many around here know Ruby's deep magic
well), and they always refer to the same memory space.

I think you're using symbols inappropriately. What -are- you doing with
them? If you need to add two names together, then my guess is, just
stick with strings.

just quickly: they start life as a method name so i was hoping they
could stay symbols - but i need a tie breaker for the one level key
that becomes a simpleton method (name) in this "whiteboard" class. i
keep trying to use all symbols. note: ruby is not fighting me with
the /?!/ - so the door seems open to bite the bullet and convert ->
append -> reconvert but it "hurts" my sense of symmetry - but thanks -
that makes perfect sense (and now that i think about it- it had to be
that way, almost).

thunk
 
A

Aldric Giacomoni

thunk said:
just quickly: they start life as a method name so i was hoping they
could stay symbols - but i need a tie breaker for the one level key
that becomes a simpleton method (name) in this "whiteboard" class. i
keep trying to use all symbols. note: ruby is not fighting me with
the /?!/ - so the door seems open to bite the bullet and convert ->
append -> reconvert but it "hurts" my sense of symmetry - but thanks -
that makes perfect sense (and now that i think about it- it had to be
that way, almost).

thunk

Well, it sounds like you're using symbols for the wrong purpose, but I
repeat myself.
You may want to take a look at this Rails bit:
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=C00000033&name=HashWithIndifferentAccess

I'm not saying you should use that, or even think in that direction. It
does however sound similar to what you're doing.

When designing something, the rule is to ask 'why' five times.

You want to use symbols.
Why?
.. They start life as a method name.

Okay, well, you can send strings to "call", not just symbols. So.. Why
do you want to use symbols?
If the answer is "Oh, because I want to avoid using strings", then the
next question is ..

Why do you want to avoid using strings? ;-)

It sounds like you're doing some strangely heavy string manipulation.
Why use symbols at all?
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I'm afraid, that is not correct; the String is created as a side
effect and stored in a global variable:

irb(main):001:0> str = " foo"
=> " foo"
irb(main):002:0> str =~ /[^ ]/
=> 3
irb(main):003:0> $&
=> "f"

And it has the disadvantage over str[/\A */].length that likely is
slower because of the alternative.
There was a blog by Yehuda Katz that implied to me that these globals
weren't set until you asked for them, but upon re-reading, it isn't really
clear to me what actually happens
http://yehudakatz.com/2010/02/25/rubys-implementation-does-not-define-its-semantics/
 
A

Aldric Giacomoni

Josh said:
And it has the disadvantage over str[/\A */].length that likely is
slower because of the alternative.
There was a blog by Yehuda Katz that implied to me that these globals
weren't set until you asked for them,

No, they are created as soon as the regular expression is parsed. I
believe Wycats explained that somewhere else, but I can't find the link
at the moment.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Josh said:
And it has the disadvantage over str[/\A */].length that likely is
slower because of the alternative.
There was a blog by Yehuda Katz that implied to me that these globals
weren't set until you asked for them,

No, they are created as soon as the regular expression is parsed. I
believe Wycats explained that somewhere else, but I can't find the link
at the moment.
I was reading Rick DeNatale's blog Talk Like A Duck which directed me to a
different post of Yehuda Katz's, where I found a more explicit explanation:
"When you use =~, there is no actual MatchData object, $1 just behaves like
it was indexing into an implicit MatchData"
http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-oriented-language/

He doesn't say anything about $&, but I'd be surprised if it wasn't the
same.
Though a quick survey of the pickaxe and The Ruby Programming Language
didn't imply this implementation detail.
 
R

Rick DeNatale

Josh said:
On Tue, Mar 30, 2010 at 9:35 AM, Robert Klemme

And it has the disadvantage over str[/\A */].length that likely is
slower because of the alternative.

There was a blog by Yehuda Katz that implied to me that these globals
weren't set until you asked for them,

No, they are created as soon as the regular expression is parsed. I
believe Wycats explained that somewhere else, but I can't find the link
at the moment.
I was reading Rick DeNatale's blog Talk Like A Duck which directed me to a
different post of Yehuda Katz's, where I found a more explicit explanation:
"When you use =~, there is no actual MatchData object, $1 just behaves like
it was indexing into an implicit MatchData"
http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-oriented-language/

He doesn't say anything about $&, but I'd be surprised if it wasn't the
same.
Though a quick survey of the pickaxe and The Ruby Programming Language
didn't imply this implementation detail.

The 'global' variables which are set by regular expression matches
aren't really global, they are 'frame local'

http://talklikeaduck.denhaven2.com/2008/11/17/in-ruby-globals-arent-always-global

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

The 'global' variables which are set by regular expression matches
aren't really global, they are 'frame local'


http://talklikeaduck.denhaven2.com/2008/11/17/in-ruby-globals-arent-always-global
Thanks, Rick, that was actually a reassuring post, I always used to make
sure and use those variables asap in case some other code somewhere else
ended up resetting them. Guess that wasn't necessary :)

Can you confirm Yehuda's quote that said those variables don't exist until
they are called for?
 
T

thunk

My generalize and filled with Ruby Goodness "WhiteBoard" designed has
dropped performance. Dang! I thought it was all my traces... this
thing has gone from peaking at 2500 (ru'ids) / sec down to 1800 or
so. But it is much more general. I even put in some "channel" logic
to cut down on redundancy. (something like Java "with" statement).
That's probably not going to make much sense...

My suspicion is that the dynamic code around creating singletons must
be slow compared to just cloning variables (so to say, end result)?

Comments on that?

I probably could have used one of those dynamic structures instead of
a "root" class. Could all the other methods be removed to tweak
things along, would that be the automatic advantage of using
structures in the first place?



....but the singletons and all are just so simple/elegant in this
thing. I have all kinds of methods to dump/trace now. I am not going
to go back! This is a quest now, but I think I will worry about
performance after the thing is fully functional.

(I remember seeing "CleanSlate" in the "Builder" Gem and that gave me
some inspiration on this... did he do that maybe for performance? I
assumed until now it was for avoiding conflicts. Does that make
sense? if so that might be an easy key to getting the lost
performance back.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top