So I'm still quite new to Ruby and so far I love it's simplicity, but I
just can't figure out why I'm getting this error?... I know its probably
something I did thats very simple because at the moment, I just woke up
so I'm still kind of "waking up"...
H:/RoboSteve/lib/RoboEngine.rb:17: syntax error, unexpected '\n',
expecting tCOLON2 or '[' or '.'
H:/RoboSteve/lib/RoboEngine.rb:28: syntax error, unexpected keyword_end,
expecting $end
Could anyone help me with this? Or tell me something I could to better
with my code?
(if you do so please explain why it's better than what I've already got.
Please hold the rude comments to yourself.)
So I'm still quite new to Ruby and so far I love it's simplicity, but I
just can't figure out why I'm getting this error?... I know its probably
something I did thats very simple because at the moment, I just woke up
so I'm still kind of "waking up"...
Then perhaps you should have waited for your morning coffee to kick in?
H:/RoboSteve/lib/RoboEngine.rb:17: syntax error, unexpected '\n',
expecting tCOLON2 or '[' or '.'
H:/RoboSteve/lib/RoboEngine.rb:28: syntax error, unexpected keyword_end,
expecting $end
The problem is the "self." in the class names.
Could anyone help me with this? Or tell me something I could to better
with my code?
(if you do so please explain why it's better than what I've already got.
Please hold the rude comments to yourself.)
This last line actually made me wonder whether I should even bother
replying. You just assume we are going to be sending you "rude
comments"? Not very nice.
···
On 24 January 2012 17:14, Paet Worlds II <paetilium@live.com> wrote:
so, there are a few things with your code that are a little 'strange.'
first - i'm not sure about defining classes as:
class self.WhatTheHeck
···
##
end
i think that's where you /n (or newline) error is coming from, and i'm
not sure that this works at all - ditch the 'self's.
second, remember that methods reside within the class you create them
in - so in line 25 when you call `thoughtp.testforaccess` from within
the Startup class, Startup has no idea what `thoughttp` is, nor what the
method `testforaccess` is.
also, in line 40, you define a method called `Steveloaded` - classes,
modules, and constants start with capital letters, not methods - this
will throw an error for sure.
i think that maybe what you're trying to do is to instantiate one
class from within another, and then call some methods on the instance of
the first class from within the other. that's no sweat... take a look
at something simple like this:
class Bicycle
def initialize
p "#{self} - i'm a 3 speed bike" @gear = 1
end
def change_gear
p "#{self} was in gear #{@gear}" @gear +=1 @gear = 1 if @gear > 3
p "and now #{self} is in gear #{@gear}"
end
end
class Automobile
def initialize
p "#{self} - i'm an automobile" @speed = 55
end
def accelerate
p "#{self} was going at #{@speed} mph" @speed += 15
p "and now #{self} is going at #{@speed} mph"
end
end
class ThingsWithWheels
def initialize
p "these are things with wheels..."
bike = Bicycle.new
car = Automobile.new
sleep(1.5)
bike.change_gear
sleep(1.5)
car.accelerate
end
end
rolling_stuff = ThingsWithWheels.new
is that something more like what you were after? notice too that i
use instance variables (variables that start with `@`) instead of global
variables (which start with `$`.) global variables are generally a bad
idea - they tend to muck up the works sometimes. instance variables can
be accessed from any method of a particular instance of a class, and
generally do the job quite well.
So I'm still quite new to Ruby and so far I love it's simplicity, but I
just can't figure out why I'm getting this error?... I know its probably
something I did thats very simple because at the moment, I just woke up
so I'm still kind of "waking up"...
Then perhaps you should have waited for your morning coffee to kick in?
I couldn't possibly hault my progress for coffee...
H:/RoboSteve/lib/RoboEngine.rb:17: syntax error, unexpected '\n',
expecting tCOLON2 or '[' or '.'
H:/RoboSteve/lib/RoboEngine.rb:28: syntax error, unexpected keyword_end,
expecting $end
The problem is the "self." in the class names.
What exactly is wrong with the self. ?o,o
Could anyone help me with this? Or tell me something I could to better
with my code?
(if you do so please explain why it's better than what I've already got.
Please hold the rude comments to yourself.)
This last line actually made me wonder whether I should even bother
replying. You just assume we are going to be sending you "rude
comments"? Not very nice.
Please don't take that personally... I haven't gotten used to the use of
these forums just yet. I'm too used getting "trolled" for being new to
things >.< Sorry.
···
On 24 January 2012 17:14, Paet Worlds II <paetilium@live.com> wrote:
also, in line 40, you define a method called `Steveloaded` - classes,
modules, and constants start with capital letters, not methods - this
will throw an error for sure.
Actually, methods *can* start with a capital letter, and although it's
not all that common, there are plenty of examples in the standard
library.
Integer("123")
=> 123
Integer "123"
=> 123
i.e. the usual case is to have a method with the same name as a class,
and use that method as a kind of constructor.
Note, however, that you have to call it with arguments, or as Foo() or
self.Foo. A bare Foo by itself is treated as a constant.
Hilco Wijbenga wrote in post #1042399:
>
> The problem is the "self." in the class names.
What exactly is wrong with the self. ?o,o
The "self" is a way to refer to the current object context. For
instance, in an irb session:
$ irb
>> self.class
=> Object
Because self already has meaning in Ruby, when you try to define a class
like this:
class self.Startup
. . . Ruby is trying to call a Startup method on the Object object (no,
that repetition wasn't accidental -- there's an object called Object) and
use the return value of that method as the name of the class you're
trying to define. Of course, the return value of it is nil, because
there is no Startup method for the Object object, so to the interpreter
it looks like you're doing this:
class
Because it's difficult to see there, I'll explain that there's a space
after "class" followed by a newline, which is why Ruby is complaining
that there's an unexpected "\n" (the escape character for a newline).
Ruby expects a class name to follow the "class" keyword.
That's what's wrong with the "self." there.
···
On Wed, Jan 25, 2012 at 10:41:10AM +0900, Paet Worlds II wrote:
So if I had a class with a method that returned a string, I could use
that where the name is expected in a class declaration? That doesn't
seem right. Tried it in irb, using both a class method (Foo.bar) and
an instance method (foo.bar), and neither worked.
-Dave
···
On Tue, Jan 24, 2012 at 23:32, Chad Perrin <code@apotheon.net> wrote:
Because self already has meaning in Ruby, when you try to define a class
like this:
class self.Startup
. . . Ruby is trying to call a Startup method on the Object object (no,
that repetition wasn't accidental -- there's an object called Object) and
use the return value of that method as the name of the class you're
trying to define.
--
Dave Aronson, Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote); details @ http://www.DaveAronson.com/\.
No, you couldn't do that either. I'm explaining why you got the error
Ruby gave you, not giving you suggestions for how to name classes in
horrible programmatic ways. If you *did* want to name a class in a
horrible programmatic way, you'd have to do something like:
bar = "Hello" + "World"
foo = <<EOF
class #{bar}
def initialize(name='World') @name = name
end
def greet
return "Hello, #{@name}!"
end
end
EOF
eval(foo)
hello = HelloWorld.new
hello.greet
There may be easier ways to achieve the same end but, in general, don't
do it. It'll probably just get your code in trouble.
···
On Thu, Jan 26, 2012 at 03:43:59AM +0900, Dave Aronson wrote:
On Tue, Jan 24, 2012 at 23:32, Chad Perrin <code@apotheon.net> wrote:
> Because self already has meaning in Ruby, when you try to define a class
> like this:
>
> class self.Startup
>
> . . . Ruby is trying to call a Startup method on the Object object (no,
> that repetition wasn't accidental -- there's an object called Object) and
> use the return value of that method as the name of the class you're
> trying to define.
So if I had a class with a method that returned a string, I could use
that where the name is expected in a class declaration? That doesn't
seem right. Tried it in irb, using both a class method (Foo.bar) and
an instance method (foo.bar), and neither worked.
No, you couldn't do that either. I'm explaining why you got the error
Ruby gave you,
Just to clarify, I wasn't the OP who got the error, I was just
interested in an odd implication of your explanation.
bar = "Hello" + "World"
foo = <<EOF
class #{bar}
def initialize(name='World') @name = name
end
def greet
return "Hello, \#\{@name\}\!"
end
end
EOF
eval(foo)
And to point out just HOW horrible that approach is, even aside from
the evil of eval... you have to remember to jump through additional
hoops and escape the #{@name}. Otherwise, it gets evaluated at
here-doc-parse time, as empty. That means hello.greet will just
return "Hello, !" (even if you did supply a name). If you escape it,
HelloWorld.new.greet properly returns "Hello, World!", and
HelloWorld.new('Dave').greet properly returns "Hello, Dave!".
Now imagine layering another level of escaping on top of it, like if
you were doing things in the class, that in turn required the actual
final code to have some escaping. Bleagh.
There may be easier ways to achieve the same end but, in general, don't
do it. It'll probably just get your code in trouble.
Amen. Metaprogramming is very powerful... but with great power etc.
-Dave
···
On Wed, Jan 25, 2012 at 22:35, Chad Perrin <code@apotheon.net> wrote:
It's incredibly rare to need to resort to string evaluation. This example
can be rewritten as:
bar = "Hello" + "World"
klass = Class.new do
def initialize(name='World') @name = name
end
def greet
return "Hello, #{@name}"
end
end
Object.const_set bar, klass
···
On Wed, Jan 25, 2012 at 9:35 PM, Chad Perrin <code@apotheon.net> wrote:
On Thu, Jan 26, 2012 at 03:43:59AM +0900, Dave Aronson wrote:
> On Tue, Jan 24, 2012 at 23:32, Chad Perrin <code@apotheon.net> wrote:
>
> > Because self already has meaning in Ruby, when you try to define a
class
> > like this:
> >
> > class self.Startup
> >
> > . . . Ruby is trying to call a Startup method on the Object object (no,
> > that repetition wasn't accidental -- there's an object called Object)
and
> > use the return value of that method as the name of the class you're
> > trying to define.
>
> So if I had a class with a method that returned a string, I could use
> that where the name is expected in a class declaration? That doesn't
> seem right. Tried it in irb, using both a class method (Foo.bar) and
> an instance method (foo.bar), and neither worked.
No, you couldn't do that either. I'm explaining why you got the error
Ruby gave you, not giving you suggestions for how to name classes in
horrible programmatic ways. If you *did* want to name a class in a
horrible programmatic way, you'd have to do something like:
bar = "Hello" + "World"
foo = <<EOF
class #{bar}
def initialize(name='World') @name = name
end
def greet
return "Hello, #{@name}!"
end
end
EOF
eval(foo)
hello = HelloWorld.new
hello.greet
There may be easier ways to achieve the same end but, in general, don't
do it. It'll probably just get your code in trouble.
> No, you couldn't do that either. I'm explaining why you got the error
> Ruby gave you,
Just to clarify, I wasn't the OP who got the error, I was just
interested in an odd implication of your explanation.
Oops. Sorry, I wasn't paying enough attention there. I'm usually pretty
good about that.
>
> There may be easier ways to achieve the same end but, in general, don't
> do it. It'll probably just get your code in trouble.
Amen. Metaprogramming is very powerful... but with great power etc.
Megaprogramming is wonderful -- when done conscientiously, and only when
you're sure you need it (or when just playing around but don't intend to
inflict the results on the public in the form of supposedly production
ready code). People really need to develop the habit of thinking about
the unintended consequences of their actions to do so even remotely
safely.
···
On Fri, Jan 27, 2012 at 05:18:53AM +0900, Dave Aronson wrote:
On Wed, Jan 25, 2012 at 22:35, Chad Perrin <code@apotheon.net> wrote:
If the class name is dynamic (meaning, it comes from some input
outside the program code) then the only reasonable thing is to derive
the definition of the class also from input. Having a class with a
dynamic name whose behavior is always defined in the same way would be
useless. Also, you would want to have several of them because
otherwise a single class with a fixed name and input driven definition
would be sufficient. If you have multiple classes with dynamic
definitions (and probably names as well, but they could also be
unnamed) then you need a way to select them. This could be something
which is used as key in a Hash (quite efficient) - but you could also
have a dynamic mechanism, where each class is asked whether it feels
responsible.
cl =
cl << Class.new do
def self.my_job?(x)
x % 2 == 0
end
def show(x)
printf "I EVEN show you %d.\n", x
end
end
cl << Class.new do
def self.my_job?(x)
x % 2 != 0
end
def show(x)
printf "%d is ODD.\n", x
end
end
irb(main):022:0> 5.times do |i|
irb(main):023:1* cls = cl.find {|c| c.my_job? i}
irb(main):024:1>
irb(main):025:1* if cls
irb(main):026:2> obj = cls.new
irb(main):027:2> obj.show i
irb(main):028:2> else
irb(main):029:2* $stderr.printf "Nobody wants %d\n", i
irb(main):030:2> end
irb(main):031:1> end
I EVEN show you 0.
1 is ODD.
I EVEN show you 2.
3 is ODD.
I EVEN show you 4.
=> 5
But all this does not seem the case for the OP's piece of code, so
we're rather abducting the thread.
Kind regards
robert
···
On Fri, Jan 27, 2012 at 7:56 AM, Chad Perrin <code@apotheon.net> wrote:
On Fri, Jan 27, 2012 at 12:20:08PM +0900, Josh Cheek wrote:
It's incredibly rare to need to resort to string evaluation. This example
can be rewritten as:
bar = "Hello" + "World"
klass = Class.new do
def initialize(name='World') @name = name
end
def greet
return "Hello, #{@name}"
end
end
Object.const_set bar, klass
You're right, that's much better -- apart from the fact I have yet to
come up with a single good reason for dynamic class names.
Not (currently) _strictly_ true. describe does make a new class and gives it a "name" but it doesn't actually bother to set_const on anything. It used to, way back in the day, but I gave it up because I couldn't decide what to do given A::C vs B::C and I didn't want anything breaking because of it.
That's not to say that dynamically creating named classes is a bad thing, just that it didn't fit in this case given the inherent complexities of tests.
···
On Jan 27, 2012, at 01:31 , Gunther Diemant wrote:
You're right, that's much better -- apart from the fact I have yet to
come up with a single good reason for dynamic class names.
Test cases in a dsl test language like MiniTest/Spec or RSpec
describe "Foo" do
end
#=> Class FooTest < MiniTest::TestCase
So . . . you are not only a trollish jackass, but also deficient in your
understanding of English. Thanks for the update.
···
On Sun, Jan 29, 2012 at 05:18:50PM +0900, Ryan Davis wrote:
On Jan 27, 2012, at 10:43 , Chad Perrin wrote:
> On Fri, Jan 27, 2012 at 06:31:38PM +0900, Gunther Diemant wrote:
>>> You're right, that's much better -- apart from the fact I have yet to
>>> come up with a single good reason for dynamic class names.
>>
>> Test cases in a dsl test language like MiniTest/Spec or RSpec
>>
>> describe "Foo" do
>> end
>> #=> Class FooTest < MiniTest::TestCase
>
> Why is that better than storing unnamed classes in a hash with keys for
> the "names" instead of actually, dynamically assigning names to new
> hashes?
<ass owner="chad">
<pitchfork>
<torch>
Your question makes no sense as worded. I can't even guess at the intent.
</torch>
</pitchfork>
</ass>
P.S. I tried to do the above in yaml and it just didn't feel as funny. Close tags make things funnier.