SitePoint Sponsor |
|
User Tag List
Results 26 to 36 of 36
-
Dec 10, 2005, 08:24 #26
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I wasn't talking about the underscore ;-)
ThisIsASampleSentenceCanYouReadIt?
this_is_a_sample_sentence_can_you_read_it?
Ruby is about readability, and the second one is much easier to read than the first. And that's why Ruby uses underscores.
PHP mixes underscores with camelcase in function names. strpos() str_replace(): hard to remember.
Compare:
Code:string = "this is a string" string.include? "string" string['string'] = "new string"
Code:$string = "this is a string"; strpos($string, 'string') !== false; str_replace("string", "new string", $string);
1. base string for strpos is the first argument
2. base string for str_replace is the last argument
3. not str_pos, but strpos and not strreplace, but str_replace
This is much easier in Ruby. Replacing a part of a string is similar to replacing part of an array. Checking if a string contains another string is similar to do such a check on an array:
Code:countries = ['england', 'united states', 'canada'] countries.include? 'england' countries[1] = 'holland'
Code:local_variable = 4 Constant = 4 @instance_variable = 4 @@class_variable = 4
Code:$localVariable = 4; // there is no global standard, so $local_variable is ok too define('Constant', 4); // function to set constant, eek $this->instance_variable = 4; // php uses $this. Ruby doesn't mix methods with properies: everything is a method for the outside world. // php doesn't have class variables?
Code:@instanceVariable
Code:public $has_many = "notes";
Code:has_many :notes
Code:public $belongs_to = array( "assoc1" => array("foreign_key"=> "weird_key"), "assoc2" => null, "assoc3" => array("conditions" => "age > 25"), );
Code:belongs_to :assoc1, :foreign_key => 'weird_key' belongs_to :assoc2 belongs_to :assoc3, :conditions => 'age > 25'
Ruby is almost always shorter and more readable.
Translate this into PHP:
Code:b = true list = ('aa'..'zz').select{b = !b}
Code:ab ad af .. zz
(1 thing per line, not:
Code:$b = true; $arr = array(); $str = 'aa'; while($str != 'zz') { $arr[] = $str; $str++; $str++; }
-
Dec 10, 2005, 11:03 #27
- Join Date
- Jan 2004
- Location
- Manchester
- Posts
- 32
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
OK, I'm starting to get the picture now. The syntax looks good, and its good to KISS. If I use the following code:
Code:isSuccess = true;
-
Dec 10, 2005, 12:12 #28
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A variable. Constants start with a capital letter.
Code:IsSuccess = true IsSuccess = false
-
Dec 10, 2005, 12:44 #29
- Join Date
- May 2004
- Location
- Wenatchee, WA
- Posts
- 107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Give this site a shot neobuddah:
http://tryruby.hobix.com/
It'll give you an interactive introduction to Ruby. Kind of fun too
I think the 'public has_many = "notes"' argument is a little misleading, as Rails is actually using functions (simply without the parenthesis), just as PHP could:
Code:has_many("notes");
Also, I believe you can capitalize class and instance variables without creating constants:
Code:@InstanceVariable @@ClassVariable
-
Dec 10, 2005, 13:01 #30
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I don't think PHP could do that with a function because PHP cannot dynamically add methods to classes from within the class definition.
-
Dec 10, 2005, 13:24 #31
- Join Date
- Jan 2004
- Location
- Manchester
- Posts
- 32
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
-
Dec 11, 2005, 02:14 #32
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Isn't PHP5's reflection read-only?
-
Dec 12, 2005, 07:14 #33
- Join Date
- Jan 2005
- Location
- Denmark
- Posts
- 1,222
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
On the model I'd agree: The model should know nothing about views or controller. It should expose properties and events for the controller or view to use as they see fit./mouse
-
Dec 12, 2005, 09:58 #34
- Join Date
- Aug 2005
- Posts
- 986
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yea, the Controller is much closer to the view than the models are. But I meant that it isn't good to code your controlles with the HTML of your views in mind. You shoudn't think: "This data is going to be displayed in a table, and not in a list, so I have to do this with it" when coding your controller. It should be possible to display the data in a table and in a list (<ul>) without making changes to the controller.
-
Dec 12, 2005, 10:20 #35
- Join Date
- Jan 2005
- Location
- Denmark
- Posts
- 1,222
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Fenrir2
Sorry that I misunderstood you.
/mouse
-
Sep 18, 2006, 21:28 #36
- Join Date
- Jan 2005
- Location
- utah
- Posts
- 11
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
so...back to the point
So, this was about php on trax, not a Rails vs. whatever debate. I think there are several websites devoted to that. Anyone actually try trax?
Bookmarks