Skip to content

Instantly share code, notes, and snippets.

@ysr23
Created May 18, 2012 13:58
Show Gist options
  • Save ysr23/2725401 to your computer and use it in GitHub Desktop.
Save ysr23/2725401 to your computer and use it in GitHub Desktop.
Grahams Homework Pt1 Numbers & Strings

#Numbers

Numbers take the form of Fixnum, Bignum (Class: Integer from Class: Numeric) or Float(Class: Numeric) there are others but they are part of the Standard Library, these are Complex, BigDecimal and Rational - these are not covered here. I can't be arsed. Sooo...

ruby-1.9.2-p0 > 5.class
 => Fixnum 
ruby-1.9.2-p0 > 5453897348957348957389457389457398.class
 => Bignum 
ruby-1.9.2-p0 > 5.0.class
 => Float 

The type is assigned by Ruby on-the-fly: it looks, it decides, you live with it... or change it later yourself. NOT ALWAYS SEE ADDENDUM

###The 2 / 3 Question###

or, why does this happen:

ruby-1.9.2-p0 > 2 / 3
 => 0 
ruby-1.9.2-p0 > 2.0 / 3.0
 => 0.6666666666666666 

Its because dividing an integer(fixnum) will return an integer so

ruby-1.9.2-p0 > 8 / 2
 => 4 

everything looks fine but

ruby-1.9.2-p0 > 9 / 2
 => 4

BAD RUBY!

To get round this i could do to_f :

ruby-1.9.2-p0 > 9.to_f / 2.to_f
 => 4.5

obv in real world i would use variables and i can see that if just one of these is a float then i get the float returned:

ruby-1.9.2-p0 > x = 9
 => 9 
ruby-1.9.2-p0 > y = 2
 => 2 
ruby-1.9.2-p0 > x / y.to_f
 => 4.5 
ruby-1.9.2-p0 > x.to_f / y
 => 4.5

there is another way i could do this and that is by invoking the Float() method on its ass

ruby-1.9.2-p0 > x / Float(y)
 => 4.5 

advantages of this over .to_f ??? who knows? Not me.

ADDENDUM So I have learned that if you are dealing with integers(Fixnum) Ruby will stay with Fixnum and never let you have the Float you crave. BUT if it decides that it wants to upgrade you to Bignum it will do that... automatic free upgrade:

ruby-1.9.2-p0 > (2 ** 30).class
 => Fixnum 
ruby-1.9.2-p0 > (2 ** 3000).class
 => Bignum 

And it may even decide it needs Floats after all, but it will warn you if you are taking the piss:

ruby-1.9.2-p0 > (2 ** 3000000000000000000000000000000).class
(irb):37: warning: in a**b, b may be too big
 => Float

Finally these are the Comparison Operators i can do with Numbers:

  • > Greater than
  • < Less than
  • == equal to
  • >= greater than OR equal to
  • <= less than OR equal to
  • <=> Comparison: 0 if they are equal 1 if the first is greater -1 if the second is higher # WTF would iuse this for???
  • != not equal to

worth mentioning is between? or &&

ruby-1.9.2-p0 > age = 15
 => 15 
ruby-1.9.2-p0 > puts  "You should be at school" if age > 5 && age < 16
You should be at school
 => nil 
ruby-1.9.2-p0 > age = 16
 => 16 
ruby-1.9.2-p0 > puts  "You should be at school" if age > 5 && age < 16
 => nil 

Strings

This is a string:

ruby-1.9.2-p0 > "hello".class
 => String 

between quotation marks y'see - but not always, i can do a multiline string:

ruby-1.9.2-p0 > s = %q{ it was
ruby-1.9.2-p0'> the best of 
ruby-1.9.2-p0'> times, It
ruby-1.9.2-p0'> was the shittiest of 
ruby-1.9.2-p0'> times}
 => " it was\nthe best of\ntimes, It\nwas the shittiest of \ntimes"
ruby-1.9.2-p0 > s.class
 => String

the curly braces above are just a delimiter i could just as easily use ^

ruby-1.9.2-p0 > s = %q^ it was
ruby-1.9.2-p0'> the best of
ruby-1.9.2-p0'> etc, etc..^
 => " it was\nthe best of\netc, etc.." 

I know balls all about anything but thats insane, BUT it will allow my string to contain any other characters and not break the string... perhaps useful then. If I am not sure what characters my frankly nuts drunken keyboard bashing users will use then i could make my own delimiter, this is called a here document:

ruby-1.9.2-p0 > s = <<END_THIS_SHIT_ALREADY 
ruby-1.9.2-p0"> it was
ruby-1.9.2-p0"> the best
ruby-1.9.2-p0"> of times
ruby-1.9.2-p0"> it was
ruby-1.9.2-p0"> END_THIS_SHIT_ALREADY
 => "it was\nthe best\nof times\nit was\n"

So, you can have quotes or you maybe can't its up to you. But you will always have quotes. eh? that %q that will be treated like single quotes where %Q will be treated like double quotes. Also above it is treating the string like it is single quoted, to treat it like double quotes do

' should be used over " maybe not for performance but as a best-practise UNLESS you are interpolating. Concatenation could be used with single quotes. Differences between concatenation and interpolation? concatenation (with +) will not change the original string interpolation will. That said concatenation with << will change the original string.

Interpolation

ruby-1.9.2-p0 > x = "woo"
 => "woo" 
ruby-1.9.2-p0 > y = "yay"
 => "yay" 
ruby-1.9.2-p0 > puts "#{x} PLUS #{y} = #{x + y}!!"
woo PLUS yay = wooyay!!
 => nil 

There are approx 34879653874million methods i can do on string .capitalize .next .reverse and i can mix and match these:

ruby-1.9.2-p0 > (x + y).capitalize.reverse
 => "yayooW"

Almost always these will return a class string, the obv exception is if i use the method to_i or similar.

A note on interpolation: String interpolation does not work with '

ruby-1.9.2-p0 > puts "wanna see a wooyay? #{x+y}"
wanna see a wooyay? wooyay
 => nil 
ruby-1.9.2-p0 > puts 'wanna see a wooyay? #{x+y}'
wanna see a wooyay? #{x+y}

Another difference is that you can include a backslash \ in single quotes ' but not in doubles " it has to be escaped \\

ruby-1.9.2-p0 > puts 'woo\yay'
woo\yay
 => nil 
ruby-1.9.2-p0 > puts "woo\yay"
wooyay
 => nil 
ruby-1.9.2-p0 > puts "woo\\yay"
woo\yay
 => nil 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment