Ruby is said to be pure OO language.
The often referred example is the abs method
irb(main):001:0> RUBY_VERSION
=> “1.6.8”
irb(main):002:0> 1.abs
=> 1
irb(main):003:0> 0.5.sin
NameError: undefined method sin' for 0.5:Float from (irb):3 irb(main):004:0> Math.sin(0.5) => 0.4794255386 irb(main):005:0> irb(main):005:0> "asdf".puts NameError: private method puts’ called for “asdf”:String
from (irb):5
Than why not sin is a method?
To fit to mathematical notation.
???
I write sin(0.5) but also abs(0.5)
So I don’t know why the difference, and why cannot a number tell it’s
sin. This is analogous to “asdf”.length and the others…
And puts?
puts is a method of IO class.
STDERR.puts “asdf”
Or you can use Kernel#display.
“asdf”.display(STDERR)
Again confusing for me…
Which approach uses ruby?
IO.puts(what) or what.puts(IO=stdout) and why it is not consistent.
The string displays itself, or the filehandle displays the string?
Or has ruby a habit in the core and not a concept?
Another ugliness in the syntax, which bothers me is the @ and @@ (and
will be @@@ ??) notation, which I think doesn’t fit into the view.
Does anybody know the root of it?
In one side ruby tries to avoid the rude $ variables, but on the other
hand has @ and @@ variables (members)…
I tend to agree with you, but some things in Ruby are just the way
they are. I don’t like that particular inconsistency, but it’s not
something that’s worth ditching the language over.
Also, if it comes to it, I can write my own which is not at all
difficult.
Than why not sin is a method?
To fit to mathematical notation.
???
I write sin(0.5) but also abs(0.5)
So I don’t know why the difference, and why cannot a number tell
it’s sin. This is analogous to “asdf”.length and the others…
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
> > Than why not sin is a method?
> To fit to mathematical notation.
???
I write sin(0.5) but also abs(0.5)
So I don't know why the difference, and why cannot a number tell it's
sin. This is analogous to "asdf".length and the others...
> > And puts?
> puts is a method of IO class.
> STDERR.puts "asdf"
> Or you can use Kernel#display.
> "asdf".display(STDERR)
Again confusing for me...
Which approach uses ruby?
IO.puts(what) or what.puts(IO=stdout) and why it is not consistent.
The string displays itself, or the filehandle displays the string?
anObj.display(io) will internally invoke io.puts(anObj)
So, a string uses an IO object to display itself.
Or has ruby a habit in the core and not a concept?
You'll see that Ruby has a concept, once you've get into it more deeply.
Another ugliness in the syntax, which bothers me is the @ and @@ (and
will be @@@ ??) notation, which I think doesn't fit into the view.
Does anybody know the root of it?
Forget Perl! $xxx and @xxx have a completely different meaning in Ruby
than in Perl.
- $xxx are global variables
- @xxx are instance variables
- @@xxx are class variables
- local variables do not have a prefix
- variables that start uppercase are constants
I like $ and @ very much, as it allows you to distinguish quickly which
variables are globals, which are instance variables and which are locals.
In C/C++ or Java you cannot determine this without looking at the
definition, which may be hundreds of lines away.
Regards,
Michael
···
On Tue, May 13, 2003 at 10:54:09PM +0900, KONTRA Gergely wrote:
Than why not sin is a method?
To fit to mathematical notation.
???
I write sin(0.5) but also abs(0.5)
So I don’t know why the difference, and why cannot a number tell it’s
sin. This is analogous to “asdf”.length and the others…
I_SUPPOSE=true
I suppose that sin as the other Math methods, is function-like
because, well, they are functions, end not properties of the Numeric
objects.
You have to chose where you’d like to put them, but think that if you
start to put math methods in Numeric you’ll have to chose when you
want to stop, or you’ll have an infinite number of function.
For the sake of clearness it’s better to have them in a separate
module
And puts?
puts is a method of IO class.
STDERR.puts “asdf”
Or you can use Kernel#display.
“asdf”.display(STDERR)
Again confusing for me…
Which approach uses ruby?
IO.puts(what) or what.puts(IO=stdout) and why it is not consistent.
The string displays itself, or the filehandle displays the string?
Or has ruby a habit in the core and not a concept?
Why a string should know about how to display herself?
You could end up putting in String every kind of IO object.
I’m not so much in OO languages , I just used Java, Ruby and little
C++ and python, su maybe having
String.display (displaying_obj) is reasonable, but sounds inefficent
to me.
Another ugliness in the syntax, which bothers me is the @ and @@ (and
will be @@@ ??) notation, which I think doesn’t fit into the view.
Does anybody know the root of it?
In one side ruby tries to avoid the rude $ variables, but on the other
hand has @ and @@ variables (members)…
Gergo
You have to clearly separate the type of the variables, to avoid
errors.
You shoul mostly use local variables, so you save a type for them, but
to avoid errors (for example overwriting a global variable or an
instance variable in a loop) ruby makes difference beetween them as
syntax.
I write sin(0.5) but also abs(0.5)
So I don’t know why the difference, and why cannot a number tell it’s
sin. This is analogous to “asdf”.length and the others…
The sin function in your math textbook was described
sin(x)
right? So I chose sin(x). I honor tradition. In my textbook,
absolute value notation was
x>
not a functional form, so I had no reason to choose functional style.
Which approach uses ruby?
IO.puts(what) or what.puts(IO=stdout) and why it is not consistent.
The string displays itself, or the filehandle displays the string?
You put text to the console line, or you display text on the console;
both does make sense, doesn’t it? Ruby users seem to prefer “puts”
though.
matz.
···
In message “Re: Ruby OO? sin method? puts method? @@?” on 03/05/13, KONTRA Gergely kgergely@mlabdial.hit.bme.hu writes:
Than why not sin is a method?
To fit to mathematical notation.
???
I write sin(0.5) but also abs(0.5)
So I don’t know why the difference, and why cannot a number tell it’s
sin. This is analogous to “asdf”.length and the others…