On Fri, May 12, 2006 at 11:27:29AM +0900, corey konrad wrote:
how about this cin>> = num, lol i dont know ruby seems like a trade off
to me so far. i am just trying to understand all this hype about it. I
mean every book i am reading says its the best most awesome easiest to
use, just like using natural language, its elegant and beautiful and it
will make you cry because its so easy to use, etc etc. I am having a
hard time understanding that. If its true i would like to see it at some
point.
Well, having to explicitly convert between strings and integers in most
cases is a concious design decision. I do still forget about it
sometimes
but luckily, the exceptions make the error easy to find.
but in ruby you only have the + operator, so in principle the result of
“200” + 1
and
200 + “1”
could be either 201 or 2001. And I’d sort of expect both cases to give
the same result. In any case, this stuff can lead to really confusing
results.
Personally, I think the way Perl handles this sort of thing is easier
for straight forward text manipulation, BUT since Perl depends on the
operator to determine the type of conversion the number of operators
is fairly large, and it becomes seriously unwieldy if you introduce
more types than simple strings and numbers. See for example the perl
table of operators for perl 6 (still under development): http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html
You can safely skip perl and java
yeah. somehow learning C or Java or maybe most languages can cause
“fear of parentheses”. learn lisp before that happens!
What’s probably going here is that you just aren’t aware of the
general suckiness of most programming languages. I have yet to see
one that I think is perfect for me or completely intuitive for
anybody; Ruby is my favorite though. Really, try doing this stuff in
C, Java, Perl, Python, Lisp, BASIC, PHP, whatever… Some are easier
to pick up on than others, but they are all extremely difficult to
start with for a complete beginner to programming in general. Just
keep at it (with almost any popular language) and you will get there.
For the record, I think that, despite your difficulties, Ruby is
probably one of the easiest languages for you and most beginners to
start programming with.
But seriously, if you feel that Ruby doesn’t work for you, check out
Python (http://www.python.org/) or maybe Perl (http://www.perl.org/)
or even Java (http://java.sun.com/). There are a lot of programming
languages out there, and it never hurts to give some of them a try.
Noooo. If you are a new programmer learn lisp and smalltalk first. If
somehow you decide that neither one of them are to your liking then
learn python.
right?
So isnt it important that a language with that as its goal can express
simple programs in a simple and intuitive way?
Intuitive to matz, yes.
I am a begining programmer and i find ruby counter intuitive so far
and that worries me and makes me wonder if i am wasting my time
with. I want to learn how to write software. Thats the bigger
picture i am in right now. Its not personal, I am just trying to
get my feet wet here and make sense of all this stuff.
You should think more about what you want and then go looking for the
appropriate documentation. You should also experiment with irb.
Try:
ri Kernel#print
ri Kernel#puts
ri Kernel#gets
ri String#to_i
irb(main):041:1> puts “this is a better number” + i_num
…
TypeError: can’t convert Fixnum into String
from (irb):41:in +' from (irb):41:infavorite_number’
from (irb):43
from :0
This happens because String#+ call #to_str on its argument – so when
you’re using `+’ to concatenate a string and another object, that object
has to respond to #to_str, and not just #to_s.
class Numeric
# this isn’t recommended, though
def to_str
to_s
end
end
Noooo. If you are a new programmer learn lisp and smalltalk first. If
somehow you decide that neither one of them are to your liking then
learn python.
You can safely skip perl and java
That’s a very broad statement. I truly enjoy perl. It’s second only to
ruby in my book.
Yeah that is the kind of example i was looking for thanks for that kick
in the ass. Thats what i need to get me excited about ruby. Java
definatly looks more complicated.
I agree completely. Java is much better for the my favorite number
program:
i just have a question i am not trying to be a smart ass either but
one thing i find strange is that the puts means put string, its a string
method, now when i use the length method to get the amount of characters
in a word it works fine…yet to put the length i get an error: cant
convert from fixnum to string problem…i have to convert the integer
returned to string even though i am using a string method? Shoulndt
string conversion be implied in the puts method since thats what people
are going to use it for or would that cause problems somehow?
This happens because String#+ call #to_str on its argument – so when
you’re using `+’ to concatenate a string and another object, that object
has to respond to #to_str, and not just #to_s.
class Numeric
# this isn’t recommended, though
def to_str
to_s
end
end
i just have a question i am not trying to be a smart ass either but
one thing i find strange is that the puts means put string, its a string
method, now when i use the length method to get the amount of characters
in a word it works fine…yet to put the length i get an error: cant
convert from fixnum to string problem…i have to convert the integer
returned to string even though i am using a string method? Shoulndt
string conversion be implied in the puts method since thats what people
are going to use it for or would that cause problems somehow?
I tried the following in IRB:
string = “fnord”
puts string.length
It printed out “5” just fine. Is that the problem you were referring to?
It printed out “5” just fine. Is that the problem you were referring to?
actually this one, i have to use + full_name.to_s + to avoid the error i
just found that strange. i mean i am using a string method the to_s
should be implicit shouldnt it? i dont know.
irb(main):030:0> def name_length
irb(main):031:1> p “what is your first name?”
irb(main):032:1> first = gets.chomp
irb(main):033:1> p “what is your last name?”
irb(main):034:1> last = gets.chomp
irb(main):035:1> full_length = first.length + last.length
irb(main):036:1> p "your full name has: " + full_length + “characters”
irb(main):037:1> end
=> nil
irb(main):038:0> name_length
“what is your first name?”
corey
“what is your last name?”
konrad
TypeError: can’t convert Fixnum into String
from (irb):36:in +' from (irb):36:inname_length’
from (irb):38
from :0
It printed out “5” just fine. Is that the problem you were referring to?
actually this one, i have to use + full_name.to_s + to avoid the error i
just found that strange. i mean i am using a string method the to_s
should be implicit shouldnt it? i dont know.
Try this:
p “Your full name has #{full_length} characters.”
The #{} evaluates everything inside it and then turns the result into
a string. It’s meant just for this sort of thing.
str + number → gives you an error, before puts even get’s around to
seeing anything
I would suggest almost always using interpolation. It will give you
what you expect, pretty much everytime. To rewrite what you have
above with string interpolation:
p “your full name has: #{name_length} characters”
oh ok thanks i guess i should read this entire book on the laguage first
before getting snooty and thinking i have everything all figured out,
lol. I tend to do things like that all the time.
It printed out “5” just fine. Is that the problem you were referring to?
actually this one, i have to use + full_name.to_s + to avoid the error i
just found that strange. i mean i am using a string method the to_s
should be implicit shouldnt it? i dont know.
Try this:
p “Your full name has #{full_length} characters.”
The #{} evaluates everything inside it and then turns the result into
a string. It’s meant just for this sort of thing.